From a87f4a2b8abb600201c2768f1d7a75a13b805bba Mon Sep 17 00:00:00 2001 From: yogevbd Date: Wed, 12 Dec 2018 15:02:28 +0200 Subject: [PATCH] Migrate GCM to FCM --- android/build.gradle | 4 +- android/src/main/AndroidManifest.xml | 33 ++------- .../RNNotificationsModule.java | 8 +-- ...java => FcmInstanceIdListenerService.java} | 26 ++++++- ...> FcmInstanceIdRefreshHandlerService.java} | 8 +-- .../gcm/{GcmToken.java => FcmToken.java} | 72 +++++-------------- .../gcm/GcmInstanceIdListenerService.java | 21 ------ .../gcm/{IGcmToken.java => IFcmToken.java} | 2 +- .../gcm/INotificationsGcmApplication.java | 2 +- package.json | 2 +- 10 files changed, 57 insertions(+), 121 deletions(-) rename android/src/main/java/com/wix/reactnativenotifications/gcm/{GcmMessageHandlerService.java => FcmInstanceIdListenerService.java} (50%) rename android/src/main/java/com/wix/reactnativenotifications/gcm/{GcmInstanceIdRefreshHandlerService.java => FcmInstanceIdRefreshHandlerService.java} (74%) rename android/src/main/java/com/wix/reactnativenotifications/gcm/{GcmToken.java => FcmToken.java} (51%) delete mode 100644 android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdListenerService.java rename android/src/main/java/com/wix/reactnativenotifications/gcm/{IGcmToken.java => IFcmToken.java} (95%) diff --git a/android/build.gradle b/android/build.gradle index de0db35..2fdb45b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -20,8 +20,8 @@ android { dependencies { // Google's GCM. - compile 'com.google.android.gms:play-services-gcm:15.0.1' - +// compile 'com.google.android.gms:play-services-gcm:15.0.1' + compile "com.google.firebase:firebase-messaging:17.3.0" compile 'com.facebook.react:react-native:+' testCompile 'junit:junit:4.12' diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 1433475..ffef75f 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -21,39 +21,16 @@ --> - - - - - - - - - + android:name=".gcm.FcmInstanceIdListenerService"> - - - - - - - + + + diff --git a/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java b/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java index 33a13bf..8a85a6b 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java +++ b/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java @@ -23,7 +23,7 @@ import com.wix.reactnativenotifications.core.notification.PushNotification; import com.wix.reactnativenotifications.core.notification.PushNotificationProps; import com.wix.reactnativenotifications.core.notificationdrawer.IPushNotificationsDrawer; import com.wix.reactnativenotifications.core.notificationdrawer.PushNotificationsDrawer; -import com.wix.reactnativenotifications.gcm.GcmInstanceIdRefreshHandlerService; +import com.wix.reactnativenotifications.gcm.FcmInstanceIdRefreshHandlerService; import static com.wix.reactnativenotifications.Defs.LOGTAG; @@ -47,7 +47,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements @Override public void initialize() { Log.d(LOGTAG, "Native module init"); - startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_IS_APP_INIT); + startGcmIntentService(FcmInstanceIdRefreshHandlerService.EXTRA_IS_APP_INIT); final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext()); notificationsDrawer.onAppInit(); @@ -56,7 +56,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements @ReactMethod public void refreshToken() { Log.d(LOGTAG, "Native method invocation: refreshToken()"); - startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_MANUAL_REFRESH); + startGcmIntentService(FcmInstanceIdRefreshHandlerService.EXTRA_MANUAL_REFRESH); } @ReactMethod @@ -138,7 +138,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements protected void startGcmIntentService(String extraFlag) { final Context appContext = getReactApplicationContext().getApplicationContext(); - final Intent tokenFetchIntent = new Intent(appContext, GcmInstanceIdRefreshHandlerService.class); + final Intent tokenFetchIntent = new Intent(appContext, FcmInstanceIdRefreshHandlerService.class); tokenFetchIntent.putExtra(extraFlag, true); appContext.startService(tokenFetchIntent); } diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java similarity index 50% rename from android/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java rename to android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java index f596655..cb7763c 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmMessageHandlerService.java +++ b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java @@ -3,16 +3,26 @@ package com.wix.reactnativenotifications.gcm; 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 com.wix.reactnativenotifications.core.notification.IPushNotification; import com.wix.reactnativenotifications.core.notification.PushNotification; +import java.util.Map; + import static com.wix.reactnativenotifications.Defs.LOGTAG; -public class GcmMessageHandlerService extends GcmListenerService { +/** + * Instance-ID + token refreshing handling service. Contacts the GCM to fetch the updated token. + * + * @author amitd + */ +public class FcmInstanceIdListenerService extends FirebaseMessagingService { @Override - public void onMessageReceived(String s, Bundle bundle) { + public void onMessageReceived(RemoteMessage message){ + Map data = message.getData(); + Bundle bundle = convertMapToBundle(data); Log.d(LOGTAG, "New message from GCM: " + bundle); try { @@ -23,4 +33,14 @@ public class GcmMessageHandlerService extends GcmListenerService { Log.v(LOGTAG, "GCM message handling aborted", e); } } + + private Bundle convertMapToBundle(Map map) { + Bundle bundle = new Bundle(); + for (Map.Entry entry : map.entrySet()) { + bundle.putString(entry.getKey(), entry.getValue()); + } + + return bundle; + } + } diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdRefreshHandlerService.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdRefreshHandlerService.java similarity index 74% rename from android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdRefreshHandlerService.java rename to android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdRefreshHandlerService.java index 3aa7aa9..8270ad6 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdRefreshHandlerService.java +++ b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdRefreshHandlerService.java @@ -3,18 +3,18 @@ package com.wix.reactnativenotifications.gcm; import android.app.IntentService; import android.content.Intent; -public class GcmInstanceIdRefreshHandlerService extends IntentService { +public class FcmInstanceIdRefreshHandlerService extends IntentService { public static String EXTRA_IS_APP_INIT = "isAppInit"; public static String EXTRA_MANUAL_REFRESH = "doManualRefresh"; - public GcmInstanceIdRefreshHandlerService() { - super(GcmInstanceIdRefreshHandlerService.class.getSimpleName()); + public FcmInstanceIdRefreshHandlerService() { + super(FcmInstanceIdRefreshHandlerService.class.getSimpleName()); } @Override protected void onHandleIntent(Intent intent) { - IGcmToken gcmToken = GcmToken.get(this); + IFcmToken gcmToken = FcmToken.get(this); if (gcmToken == null) { return; } diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmToken.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmToken.java similarity index 51% rename from android/src/main/java/com/wix/reactnativenotifications/gcm/GcmToken.java rename to android/src/main/java/com/wix/reactnativenotifications/gcm/FcmToken.java index b11a6b5..44c8ec6 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmToken.java +++ b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmToken.java @@ -1,41 +1,38 @@ package com.wix.reactnativenotifications.gcm; import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.support.annotation.NonNull; import android.util.Log; import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; import com.facebook.react.bridge.ReactContext; import com.facebook.react.modules.core.DeviceEventManagerModule; -import com.google.android.gms.gcm.GoogleCloudMessaging; -import com.google.android.gms.iid.InstanceID; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.InstanceIdResult; -import static com.wix.reactnativenotifications.Defs.GCM_SENDER_ID_ATTR_NAME; import static com.wix.reactnativenotifications.Defs.LOGTAG; import static com.wix.reactnativenotifications.Defs.TOKEN_RECEIVED_EVENT_NAME; -public class GcmToken implements IGcmToken { +public class FcmToken implements IFcmToken { final protected Context mAppContext; protected static String sToken; - protected GcmToken(Context appContext) { + protected FcmToken(Context appContext) { if (!(appContext instanceof ReactApplication)) { throw new IllegalStateException("Application instance isn't a react-application"); } mAppContext = appContext; } - public static IGcmToken get(Context context) { + public static IFcmToken get(Context context) { Context appContext = context.getApplicationContext(); if (appContext instanceof INotificationsGcmApplication) { - return ((INotificationsGcmApplication) appContext).getGcmToken(context); + return ((INotificationsGcmApplication) appContext).getFcmToken(context); } - return new GcmToken(appContext); + return new FcmToken(appContext); } @Override @@ -73,51 +70,14 @@ public class GcmToken implements IGcmToken { } protected void refreshToken() { - try { - sToken = getNewToken(); - } catch (Exception e) { - Log.e(LOGTAG, "Failed to retrieve new token", e); - return; - } - - sendTokenToJS(); - } - - @NonNull - protected String getNewToken() throws Exception { - final InstanceID instanceId = InstanceID.getInstance(mAppContext); - Log.d(LOGTAG, "GCM is refreshing token... instanceId=" + instanceId.getId()); - - // TODO why is this needed? - GoogleCloudMessaging.getInstance(mAppContext).close(); - - try { - final String registrationToken = instanceId.getToken(getSenderId(), GoogleCloudMessaging.INSTANCE_ID_SCOPE); - Log.i(LOGTAG, "GCM has a new token: instanceId=" + instanceId.getId() + ", token=" + registrationToken); - return registrationToken; - } catch (Exception e) { - throw new Exception("FATAL: Failed to fetch a fresh new token, instanceId=" + instanceId.getId(), e); - } - } - - protected String getSenderId() { - final String senderId = getSenderIdFromManifest(); - if (senderId == null) { - throw new IllegalStateException("Sender ID not found in manifest. Did you forget to add it as the value of a '"+GCM_SENDER_ID_ATTR_NAME+"' meta-data field?"); - } - return senderId; - } - - protected String getSenderIdFromManifest() { - final ApplicationInfo appInfo; - try { - appInfo = mAppContext.getPackageManager().getApplicationInfo(mAppContext.getPackageName(), PackageManager.GET_META_DATA); - return appInfo.metaData.getString(GCM_SENDER_ID_ATTR_NAME); - } catch (PackageManager.NameNotFoundException e) { - // Should REALLY never happen cause we're querying for our own package. - Log.e(LOGTAG, "Failed to resolve sender ID from manifest", e); - return null; - } + FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( new OnSuccessListener() { + @Override + public void onSuccess(InstanceIdResult instanceIdResult) { + sToken = instanceIdResult.getToken(); + Log.i(LOGTAG, "FCM has a new token" + "=" + sToken); + sendTokenToJS(); + } + }); } protected void sendTokenToJS() { diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdListenerService.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdListenerService.java deleted file mode 100644 index 933415f..0000000 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/GcmInstanceIdListenerService.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.wix.reactnativenotifications.gcm; - -import android.content.Intent; - -import com.google.android.gms.iid.InstanceIDListenerService; - -/** - * Instance-ID + token refreshing handling service. Contacts the GCM to fetch the updated token. - * - * @author amitd - */ -public class GcmInstanceIdListenerService extends InstanceIDListenerService { - - @Override - public void onTokenRefresh() { - // Fetch updated Instance ID token and notify our app's server of any changes (if applicable). - // Google recommends running this from an intent service. - Intent intent = new Intent(this, GcmInstanceIdRefreshHandlerService.class); - startService(intent); - } -} diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/IGcmToken.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/IFcmToken.java similarity index 95% rename from android/src/main/java/com/wix/reactnativenotifications/gcm/IGcmToken.java rename to android/src/main/java/com/wix/reactnativenotifications/gcm/IFcmToken.java index f324a59..9e75d39 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/IGcmToken.java +++ b/android/src/main/java/com/wix/reactnativenotifications/gcm/IFcmToken.java @@ -1,6 +1,6 @@ package com.wix.reactnativenotifications.gcm; -public interface IGcmToken { +public interface IFcmToken { /** * Handle an event where we've been notified of a that a fresh token is now available from Google. diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/INotificationsGcmApplication.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/INotificationsGcmApplication.java index 36f59f7..d318ecc 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/INotificationsGcmApplication.java +++ b/android/src/main/java/com/wix/reactnativenotifications/gcm/INotificationsGcmApplication.java @@ -3,5 +3,5 @@ package com.wix.reactnativenotifications.gcm; import android.content.Context; public interface INotificationsGcmApplication { - IGcmToken getGcmToken(Context context); + IFcmToken getFcmToken(Context context); } diff --git a/package.json b/package.json index 1f65772..75bc131 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-notifications", - "version": "1.1.23", + "version": "1.2.0", "description": "Advanced Push Notifications (Silent, interactive notifications) for iOS & Android", "author": "Lidan Hifi ", "license": "MIT", -- 2.26.2