diff --git a/android/build.gradle b/android/build.gradle index de0db35f9d7c22a6d8a179f04fc39dafa1275214..2fdb45b1175a62a8df25a1d48d5d609484230472 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 14334752383c271cef67a598c6282e09dcbd8885..ffef75f236e30d1e2d4b99280dcb04964b70afd9 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 33a13bf94c95b8070bcb9cdd65fcceefa1e8c2ce..8a85a6bed6ae6c8c771c35a940026b32f0cae606 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 f59665529429b87779fc37fe009f7505b30bc394..cb7763c7293e10567c6107bb0b250fe5ead68dec 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 3aa7aa9dc8e248d2fa10ef93f3b296ec158e9b93..8270ad68e724f6463bee122fb17462c0a6a18adb 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 b11a6b5f4a6a89342d5d684bd0ed2d7950ec5991..44c8ec6dffe65f122ad21a4f51c2438fd2f11a31 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 933415f5cdc3b96b055c2a5e1c76208e914dc9b6..0000000000000000000000000000000000000000 --- 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 f324a591f64f0324be8132d41243c18b7b12f60f..9e75d39014c1a21bce7f854a3e0aa2d97abeaf0c 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 36f59f71cf2e4682d409c1fd21ac07a333d66a11..d318ecc4a9e1b78c99ce43875459c5d841426ad5 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 1f657724d714a2100d68451b90a3cb872c6b9b45..75bc131d9418551efaf94a04495ad66972f26ef5 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",