migrate to fcm library

This commit is contained in:
Daniel Gultsch 2018-05-19 20:05:45 +02:00
parent 2230d5a42c
commit ec54a483f8
7 changed files with 31 additions and 51 deletions

View File

@ -33,7 +33,7 @@ ext {
} }
dependencies { dependencies {
playstoreImplementation 'com.google.android.gms:play-services-gcm:15.0.1' playstoreImplementation 'com.google.firebase:firebase-messaging:15.0.2'
implementation 'org.sufficientlysecure:openpgp-api:10.0' implementation 'org.sufficientlysecure:openpgp-api:10.0'
implementation 'com.soundcloud.android:android-crop:1.0.1@aar' implementation 'com.soundcloud.android:android-crop:1.0.1@aar'
implementation "com.android.support:support-v13:$supportLibVersion" implementation "com.android.support:support-v13:$supportLibVersion"

View File

@ -53,7 +53,6 @@ import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -148,8 +147,8 @@ public class XmppConnectionService extends Service {
public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error"; public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error";
public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_TRY_AGAIN = "try_again";
public static final String ACTION_IDLE_PING = "idle_ping"; public static final String ACTION_IDLE_PING = "idle_ping";
public static final String ACTION_GCM_TOKEN_REFRESH = "gcm_token_refresh"; public static final String ACTION_FCM_TOKEN_REFRESH = "fcm_token_refresh";
public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received"; public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received";
private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
static { static {
@ -639,17 +638,17 @@ public class XmppConnectionService extends Service {
refreshAllPresences(); refreshAllPresences();
} }
break; break;
case ACTION_GCM_TOKEN_REFRESH: case ACTION_FCM_TOKEN_REFRESH:
refreshAllGcmTokens(); refreshAllFcmTokens();
break; break;
case ACTION_IDLE_PING: case ACTION_IDLE_PING:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scheduleNextIdlePing(); scheduleNextIdlePing();
} }
break; break;
case ACTION_GCM_MESSAGE_RECEIVED: case ACTION_FCM_MESSAGE_RECEIVED:
Log.d(Config.LOGTAG, "gcm push message arrived in service. extras=" + intent.getExtras());
pushedAccountHash = intent.getStringExtra("account"); pushedAccountHash = intent.getStringExtra("account");
Log.d(Config.LOGTAG, "push message arrived in service. account=" + pushedAccountHash);
break; break;
case Intent.ACTION_SEND: case Intent.ACTION_SEND:
Uri uri = intent.getData(); Uri uri = intent.getData();
@ -3402,7 +3401,7 @@ public class XmppConnectionService extends Service {
} }
} }
private void refreshAllGcmTokens() { private void refreshAllFcmTokens() {
for (Account account : getAccounts()) { for (Account account : getAccounts()) {
if (account.isOnlineAndConnected() && mPushManagementService.available(account)) { if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
mPushManagementService.registerPushTokenOnServer(account); mPushManagementService.registerPushTokenOnServer(account);

View File

@ -3,22 +3,8 @@
package="eu.siacs.conversations" package="eu.siacs.conversations"
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<permission android:name="eu.siacs.conversations.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="eu.siacs.conversations.permission.C2D_MESSAGE"/>
<application> <application>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<receiver android:name=".services.MaintenanceReceiver" <receiver android:name=".services.MaintenanceReceiver"
android:exported="true" android:exported="true"
android:permission="android.permission.CHANGE_CONFIGURATION"> android:permission="android.permission.CHANGE_CONFIGURATION">
@ -31,13 +17,13 @@
android:name=".services.PushMessageReceiver" android:name=".services.PushMessageReceiver"
android:exported="false" > android:exported="false" >
<intent-filter> <intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter> </intent-filter>
</service> </service>
<service android:name=".services.InstanceIdService" android:exported="false"> <service android:name=".services.InstanceIdService">
<intent-filter> <intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter> </intent-filter>
</service> </service>
</application> </application>

View File

@ -2,14 +2,14 @@ package eu.siacs.conversations.services;
import android.content.Intent; import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService; import com.google.firebase.iid.FirebaseInstanceIdService;
public class InstanceIdService extends InstanceIDListenerService { public class InstanceIdService extends FirebaseInstanceIdService {
@Override @Override
public void onTokenRefresh() { public void onTokenRefresh() {
Intent intent = new Intent(this, XmppConnectionService.class); Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
startService(intent); startService(intent);
} }
} }

View File

@ -5,7 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import com.google.android.gms.iid.InstanceID; import com.google.firebase.iid.FirebaseInstanceId;
import java.io.IOException; import java.io.IOException;
@ -23,11 +23,10 @@ public class MaintenanceReceiver extends BroadcastReceiver {
private void renewInstanceToken(final Context context) { private void renewInstanceToken(final Context context) {
new Thread(() -> { new Thread(() -> {
InstanceID instanceID = InstanceID.getInstance(context);
try { try {
instanceID.deleteInstanceID(); FirebaseInstanceId.getInstance().deleteInstanceId();
Intent intent = new Intent(context, XmppConnectionService.class); Intent intent = new Intent(context, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
context.startService(intent); context.startService(intent);
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG, "unable to renew instance token", e); Log.d(Config.LOGTAG, "unable to renew instance token", e);

View File

@ -1,15 +1,12 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.provider.Settings;
import android.util.Log; import android.util.Log;
import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.firebase.iid.FirebaseInstanceId;
import com.google.android.gms.iid.InstanceID;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
@ -31,7 +28,7 @@ public class PushManagementService {
void registerPushTokenOnServer(final Account account) { void registerPushTokenOnServer(final Account account) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support"); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support");
retrieveGcmInstanceToken(token -> { retrieveFcmInstanceToken(token -> {
final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService); final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService);
IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(APP_SERVER, token, androidId); IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(APP_SERVER, token, androidId);
mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> { mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
@ -69,14 +66,12 @@ public class PushManagementService {
}); });
} }
private void retrieveGcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) { private void retrieveFcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) {
new Thread(() -> { new Thread(() -> {
InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService);
try { try {
String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); instanceTokenRetrieved.onGcmInstanceTokenRetrieved(FirebaseInstanceId.getInstance().getToken());
instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token);
} catch (Exception e) { } catch (Exception e) {
Log.d(Config.LOGTAG, "unable to get push token"); Log.d(Config.LOGTAG, "unable to get push token",e);
} }
}).start(); }).start();

View File

@ -1,20 +1,21 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService; import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import eu.siacs.conversations.Config; import java.util.Map;
public class PushMessageReceiver extends GcmListenerService { public class PushMessageReceiver extends FirebaseMessagingService {
@Override @Override
public void onMessageReceived(String from, Bundle data) { public void onMessageReceived(RemoteMessage message) {
Map<String, String> data = message.getData();
Intent intent = new Intent(this, XmppConnectionService.class); Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_GCM_MESSAGE_RECEIVED); intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED);
intent.replaceExtras(data); intent.putExtra("account", data.get("account"));
startService(intent); startService(intent);
} }
} }