From f27a10841c8b32f5bd3a6f6ebe309ac2db097092 Mon Sep 17 00:00:00 2001 From: Brice Powell Date: Thu, 9 May 2019 10:29:39 -0400 Subject: [PATCH 1/2] Fix MyFirebaseInstanceID service issue, add abiility to delete instance ID Change name --- README.md | 11 +++++ package-lock.json | 5 +++ plugin.xml | 44 ++++++++----------- src/android/FCMPlugin.java | 46 +++++++++++++------- src/android/MyFirebaseInstanceIDService.java | 25 ----------- src/android/MyFirebaseMessagingService.java | 6 +++ src/ios/FCMPlugin.h | 1 + src/ios/FCMPlugin.m | 31 ++++++++----- www/FCMPlugin.js | 3 ++ 9 files changed, 96 insertions(+), 76 deletions(-) create mode 100644 package-lock.json delete mode 100644 src/android/MyFirebaseInstanceIDService.java diff --git a/README.md b/README.md index fb325d53d..5c5404c25 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,18 @@ Send a push notification to a single device or topic. - If the user taps the notification, the application comes to foreground and the notification data is received in the JavaScript callback. - If the user does not tap the notification but opens the applicacion, nothing happens until the notification is tapped. +## Delete InstanceId +```javascript +// Stops device from receiving Firebase notifications, returns a promise +// Note: Deleting the Firebase instance triggers a token refresh, +// have to prevent the refreshed token from being sent to the server to stop notifications +FCMPlugin.deleteInstanceId().then(()=>{ + // success +}, err => { + //error +}) +``` ## License ``` The MIT License diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..66db7eed3 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "cordova-plugin-fcm", + "version": "2.1.2", + "lockfileVersion": 1 +} diff --git a/plugin.xml b/plugin.xml index 3d6ccae6f..e22e418f5 100644 --- a/plugin.xml +++ b/plugin.xml @@ -24,12 +24,12 @@ Cordova FCM Plugin Apache 2.0 cordova, fcm, push, plugin - + Cordova FCM plugin v2.1.2 installed For more details visit https://github.com/fechanique/cordova-plugin-fcm - + @@ -37,26 +37,21 @@ - + - - + + - + - - - - - @@ -64,18 +59,17 @@ - + - + - + - - + @@ -83,7 +77,7 @@ - + development @@ -91,18 +85,18 @@ production - + - + - + - + - + @@ -113,7 +107,7 @@ - + @@ -122,9 +116,9 @@ - + - + diff --git a/src/android/FCMPlugin.java b/src/android/FCMPlugin.java index c23b9c762..4f8eff3d9 100644 --- a/src/android/FCMPlugin.java +++ b/src/android/FCMPlugin.java @@ -15,20 +15,21 @@ import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.iid.FirebaseInstanceId; + import java.util.Map; public class FCMPlugin extends CordovaPlugin { - + private static final String TAG = "FCMPlugin"; - + public static CordovaWebView gWebView; public static String notificationCallBack = "FCMPlugin.onNotificationReceived"; public static String tokenRefreshCallBack = "FCMPlugin.onTokenRefreshReceived"; public static Boolean notificationCallBackReady = false; public static Map lastPush = null; - + public FCMPlugin() {} - + public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); gWebView = webView; @@ -36,24 +37,37 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) { FirebaseMessaging.getInstance().subscribeToTopic("android"); FirebaseMessaging.getInstance().subscribeToTopic("all"); } - + public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { Log.d(TAG,"==> FCMPlugin execute: "+ action); - + try{ // READY // if (action.equals("ready")) { // callbackContext.success(); } + else if (action.equals("deleteInstanceId")) { + cordova.getThreadPool().execute(new Runnable() { + + public void run() { + try { + FirebaseInstanceId.getInstance().deleteInstanceId(); + callbackContext.success(); + } catch (Exception e) { + callbackContext.error(e.getMessage()); + } + } + }); + } // GET TOKEN // else if (action.equals("getToken")) { - cordova.getActivity().runOnUiThread(new Runnable() { + cordova.getThreadPool().execute(new Runnable() { public void run() { try{ - String token = FirebaseInstanceId.getInstance().getToken(); - callbackContext.success( FirebaseInstanceId.getInstance().getToken() ); + String token = FirebaseInstanceId.getInstance().getInstanceId(); + callbackContext.success( FirebaseInstanceId.getInstance().getInstanceId() ); Log.d(TAG,"\tToken: "+ token); }catch(Exception e){ Log.d(TAG,"\tError retrieving token"); @@ -64,7 +78,7 @@ public void run() { // NOTIFICATION CALLBACK REGISTER // else if (action.equals("registerNotification")) { notificationCallBackReady = true; - cordova.getActivity().runOnUiThread(new Runnable() { + cordova.getThreadPool().execute(new Runnable() { public void run() { if(lastPush != null) FCMPlugin.sendPushPayload( lastPush ); lastPush = null; @@ -106,21 +120,21 @@ public void run() { callbackContext.error(e.getMessage()); return false; } - + //cordova.getThreadPool().execute(new Runnable() { // public void run() { // // // } //}); - - //cordova.getActivity().runOnUiThread(new Runnable() { + + //cordova.getThreadPool().execute(new Runnable() { // public void run() { // // // } //}); return true; } - + public static void sendPushPayload(Map payload) { Log.d(TAG, "==> FCMPlugin sendPushPayload"); Log.d(TAG, "\tnotificationCallBackReady: " + notificationCallBackReady); @@ -154,10 +168,10 @@ public static void sendTokenRefresh(String token) { Log.d(TAG, "\tERROR sendRefreshToken: " + e.getMessage()); } } - + @Override public void onDestroy() { gWebView = null; notificationCallBackReady = false; } -} +} diff --git a/src/android/MyFirebaseInstanceIDService.java b/src/android/MyFirebaseInstanceIDService.java deleted file mode 100644 index 8fb3ff123..000000000 --- a/src/android/MyFirebaseInstanceIDService.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.gae.scaffolder.plugin; - -import android.util.Log; - -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.iid.FirebaseInstanceIdService; - -/** - * Created by Felipe Echanique on 08/06/2016. - */ -public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { - - private static final String TAG = "FCMPlugin"; - - @Override - public void onTokenRefresh(){ - // Get updated InstanceID token. - String refreshedToken = FirebaseInstanceId.getInstance().getToken(); - Log.d(TAG, "Refreshed token: " + refreshedToken); - FCMPlugin.sendTokenRefresh( refreshedToken ); - - // TODO: Implement this method to send any registration to your app's servers. - //sendRegistrationToServer(refreshedToken); - } -} diff --git a/src/android/MyFirebaseMessagingService.java b/src/android/MyFirebaseMessagingService.java index 941e7ca75..ab62fa88c 100644 --- a/src/android/MyFirebaseMessagingService.java +++ b/src/android/MyFirebaseMessagingService.java @@ -21,6 +21,12 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "FCMPlugin"; + @Override + public void onNewToken(String s) { + super.onNewToken(s); + Log.d("NEW_TOKEN",s); + } + /** * Called when message is received. * diff --git a/src/ios/FCMPlugin.h b/src/ios/FCMPlugin.h index be65fdcb7..4ca27df58 100644 --- a/src/ios/FCMPlugin.h +++ b/src/ios/FCMPlugin.h @@ -14,6 +14,7 @@ - (void)registerNotification:(CDVInvokedUrlCommand*)command; - (void)notifyOfMessage:(NSData*) payload; - (void)notifyOfTokenRefresh:(NSString*) token; +- (void)unregister:(CDVInvokedUrlCommand*)command; - (void)appEnterBackground; - (void)appEnterForeground; diff --git a/src/ios/FCMPlugin.m b/src/ios/FCMPlugin.m index e4edb1613..f6096973e 100644 --- a/src/ios/FCMPlugin.m +++ b/src/ios/FCMPlugin.m @@ -20,7 +20,7 @@ @implementation FCMPlugin static FCMPlugin *fcmPluginInstance; + (FCMPlugin *) fcmPlugin { - + return fcmPluginInstance; } @@ -29,16 +29,16 @@ - (void) ready:(CDVInvokedUrlCommand *)command NSLog(@"Cordova view ready"); fcmPluginInstance = self; [self.commandDelegate runInBackground:^{ - + CDVPluginResult* pluginResult = nil; pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; - + } // GET TOKEN // -- (void) getToken:(CDVInvokedUrlCommand *)command +- (void) getToken:(CDVInvokedUrlCommand *)command { NSLog(@"get Token"); [self.commandDelegate runInBackground:^{ @@ -50,7 +50,7 @@ - (void) getToken:(CDVInvokedUrlCommand *)command } // UN/SUBSCRIBE TOPIC // -- (void) subscribeToTopic:(CDVInvokedUrlCommand *)command +- (void) subscribeToTopic:(CDVInvokedUrlCommand *)command { NSString* topic = [command.arguments objectAtIndex:0]; NSLog(@"subscribe To Topic %@", topic); @@ -62,7 +62,7 @@ - (void) subscribeToTopic:(CDVInvokedUrlCommand *)command }]; } -- (void) unsubscribeFromTopic:(CDVInvokedUrlCommand *)command +- (void) unsubscribeFromTopic:(CDVInvokedUrlCommand *)command { NSString* topic = [command.arguments objectAtIndex:0]; NSLog(@"unsubscribe From Topic %@", topic); @@ -77,13 +77,13 @@ - (void) unsubscribeFromTopic:(CDVInvokedUrlCommand *)command - (void) registerNotification:(CDVInvokedUrlCommand *)command { NSLog(@"view registered for notifications"); - + notificatorReceptorReady = YES; NSData* lastPush = [AppDelegate getLastPush]; if (lastPush != nil) { [FCMPlugin.fcmPlugin notifyOfMessage:lastPush]; } - + CDVPluginResult* pluginResult = nil; pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -94,7 +94,7 @@ -(void) notifyOfMessage:(NSData *)payload NSString *JSONString = [[NSString alloc] initWithBytes:[payload bytes] length:[payload length] encoding:NSUTF8StringEncoding]; NSString * notifyJS = [NSString stringWithFormat:@"%@(%@);", notificationCallback, JSONString]; NSLog(@"stringByEvaluatingJavaScriptFromString %@", notifyJS); - + if ([self.webView respondsToSelector:@selector(stringByEvaluatingJavaScriptFromString:)]) { [(UIWebView *)self.webView stringByEvaluatingJavaScriptFromString:notifyJS]; } else { @@ -106,7 +106,7 @@ -(void) notifyOfTokenRefresh:(NSString *)token { NSString * notifyJS = [NSString stringWithFormat:@"%@('%@');", tokenRefreshCallback, token]; NSLog(@"stringByEvaluatingJavaScriptFromString %@", notifyJS); - + if ([self.webView respondsToSelector:@selector(stringByEvaluatingJavaScriptFromString:)]) { [(UIWebView *)self.webView stringByEvaluatingJavaScriptFromString:notifyJS]; } else { @@ -114,6 +114,17 @@ -(void) notifyOfTokenRefresh:(NSString *)token } } +- (void)deleteInstanceId:(CDVInvokedUrlCommand *)command { + [[FIRInstanceID instanceID] deleteIDWithHandler:^void(NSError *_Nullable error) { + if (error) { + NSLog(@"Unable to delete instance"); + } else { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + }]; +} + -(void) appEnterBackground { NSLog(@"Set state background"); diff --git a/www/FCMPlugin.js b/www/FCMPlugin.js index 149ad5303..81c2c7957 100644 --- a/www/FCMPlugin.js +++ b/www/FCMPlugin.js @@ -36,6 +36,9 @@ FCMPlugin.prototype.onTokenRefreshReceived = function(token){ console.log("Received token refresh") console.log(token) } +FCMPlugin.prototype.deleteInstanceId = function (success, error) { + exec(success, error, "FCMPlugin", "deleteInstanceId", []); +} // FIRE READY // exec(function(result){ console.log("FCMPlugin Ready OK") }, function(result){ console.log("FCMPlugin Ready ERROR") }, "FCMPlugin",'ready',[]); From c132a30bd4451bc8e759699eac1a95b626ab422e Mon Sep 17 00:00:00 2001 From: Brice Powell Date: Thu, 9 May 2019 11:21:09 -0400 Subject: [PATCH 2/2] Change getInstanceId back to getToken --- src/android/FCMPlugin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/FCMPlugin.java b/src/android/FCMPlugin.java index 4f8eff3d9..be0dee0dc 100644 --- a/src/android/FCMPlugin.java +++ b/src/android/FCMPlugin.java @@ -66,8 +66,8 @@ else if (action.equals("getToken")) { cordova.getThreadPool().execute(new Runnable() { public void run() { try{ - String token = FirebaseInstanceId.getInstance().getInstanceId(); - callbackContext.success( FirebaseInstanceId.getInstance().getInstanceId() ); + String token = FirebaseInstanceId.getInstance().getToken(); + callbackContext.success( FirebaseInstanceId.getInstance().getToken() ); Log.d(TAG,"\tToken: "+ token); }catch(Exception e){ Log.d(TAG,"\tError retrieving token");