From ec54a483f82bae7076a94557d4a6ccb3be494236 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 19 May 2018 20:05:45 +0200 Subject: [PATCH] migrate to fcm library --- build.gradle | 2 +- .../services/XmppConnectionService.java | 15 +++++++------- src/playstore/AndroidManifest.xml | 20 +++---------------- .../services/InstanceIdService.java | 6 +++--- .../services/MaintenanceReceiver.java | 7 +++---- .../services/PushManagementService.java | 15 +++++--------- .../services/PushMessageReceiver.java | 17 ++++++++-------- 7 files changed, 31 insertions(+), 51 deletions(-) diff --git a/build.gradle b/build.gradle index e1de2bdc4..0c66996f9 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,7 @@ ext { } 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 'com.soundcloud.android:android-crop:1.0.1@aar' implementation "com.android.support:support-v13:$supportLibVersion" diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 7d1c78c0f..77f89f385 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -53,7 +53,6 @@ import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import java.util.Locale; import java.util.Map; import java.util.Set; 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_TRY_AGAIN = "try_again"; 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_GCM_MESSAGE_RECEIVED = "gcm_message_received"; + public static final String ACTION_FCM_TOKEN_REFRESH = "fcm_token_refresh"; + public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received"; private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; static { @@ -639,17 +638,17 @@ public class XmppConnectionService extends Service { refreshAllPresences(); } break; - case ACTION_GCM_TOKEN_REFRESH: - refreshAllGcmTokens(); + case ACTION_FCM_TOKEN_REFRESH: + refreshAllFcmTokens(); break; case ACTION_IDLE_PING: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { scheduleNextIdlePing(); } break; - case ACTION_GCM_MESSAGE_RECEIVED: - Log.d(Config.LOGTAG, "gcm push message arrived in service. extras=" + intent.getExtras()); + case ACTION_FCM_MESSAGE_RECEIVED: pushedAccountHash = intent.getStringExtra("account"); + Log.d(Config.LOGTAG, "push message arrived in service. account=" + pushedAccountHash); break; case Intent.ACTION_SEND: Uri uri = intent.getData(); @@ -3402,7 +3401,7 @@ public class XmppConnectionService extends Service { } } - private void refreshAllGcmTokens() { + private void refreshAllFcmTokens() { for (Account account : getAccounts()) { if (account.isOnlineAndConnected() && mPushManagementService.available(account)) { mPushManagementService.registerPushTokenOnServer(account); diff --git a/src/playstore/AndroidManifest.xml b/src/playstore/AndroidManifest.xml index f4e534550..aaa43c1ff 100644 --- a/src/playstore/AndroidManifest.xml +++ b/src/playstore/AndroidManifest.xml @@ -3,22 +3,8 @@ package="eu.siacs.conversations" xmlns:android="http://schemas.android.com/apk/res/android"> - - - - - - - - - - @@ -31,13 +17,13 @@ android:name=".services.PushMessageReceiver" android:exported="false" > - + - + - + diff --git a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java index dc0804305..fdd2381dd 100644 --- a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java +++ b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java @@ -2,14 +2,14 @@ package eu.siacs.conversations.services; 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 public void onTokenRefresh() { Intent intent = new Intent(this, XmppConnectionService.class); - intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); + intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); startService(intent); } } diff --git a/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java b/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java index 24b28e859..e12c1e157 100644 --- a/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java @@ -5,7 +5,7 @@ import android.content.Context; import android.content.Intent; import android.util.Log; -import com.google.android.gms.iid.InstanceID; +import com.google.firebase.iid.FirebaseInstanceId; import java.io.IOException; @@ -23,11 +23,10 @@ public class MaintenanceReceiver extends BroadcastReceiver { private void renewInstanceToken(final Context context) { new Thread(() -> { - InstanceID instanceID = InstanceID.getInstance(context); try { - instanceID.deleteInstanceID(); + FirebaseInstanceId.getInstance().deleteInstanceId(); Intent intent = new Intent(context, XmppConnectionService.class); - intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); + intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); context.startService(intent); } catch (IOException e) { Log.d(Config.LOGTAG, "unable to renew instance token", e); diff --git a/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java b/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java index a22ac0750..022a4ff9e 100644 --- a/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java +++ b/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java @@ -1,15 +1,12 @@ package eu.siacs.conversations.services; -import android.provider.Settings; import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.gcm.GoogleCloudMessaging; -import com.google.android.gms.iid.InstanceID; +import com.google.firebase.iid.FirebaseInstanceId; import eu.siacs.conversations.Config; -import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.xml.Element; @@ -31,7 +28,7 @@ public class PushManagementService { void registerPushTokenOnServer(final Account account) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support"); - retrieveGcmInstanceToken(token -> { + retrieveFcmInstanceToken(token -> { final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService); IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(APP_SERVER, token, androidId); 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(() -> { - InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService); try { - String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); - instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token); + instanceTokenRetrieved.onGcmInstanceTokenRetrieved(FirebaseInstanceId.getInstance().getToken()); } catch (Exception e) { - Log.d(Config.LOGTAG, "unable to get push token"); + Log.d(Config.LOGTAG, "unable to get push token",e); } }).start(); diff --git a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java index 37c95e135..f6efa9b0d 100644 --- a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java @@ -1,20 +1,21 @@ package eu.siacs.conversations.services; 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 - public void onMessageReceived(String from, Bundle data) { + public void onMessageReceived(RemoteMessage message) { + Map data = message.getData(); Intent intent = new Intent(this, XmppConnectionService.class); - intent.setAction(XmppConnectionService.ACTION_GCM_MESSAGE_RECEIVED); - intent.replaceExtras(data); + intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED); + intent.putExtra("account", data.get("account")); startService(intent); } + }