diff --git a/README.md b/README.md index bffd760a5..e525aa080 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,13 @@ Unsubscribe from a topic: window.FirebasePlugin.unsubscribe("example"); ``` +### unregister + +Unregister from firebase, used to stop receiving push notifications. Call this when you logout user from your app. : +``` +window.FirebasePlugin.unregister(); +``` + ### logEvent Log an event using Analytics: diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index c986ec6fe..a8867b13a 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -87,6 +87,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else if (action.equals("unsubscribe")) { this.unsubscribe(callbackContext, args.getString(0)); return true; + } else if (action.equals("unregister")) { + this.unregister(callbackContext); + return true; } else if (action.equals("onNotificationOpen")) { this.onNotificationOpen(callbackContext); return true; @@ -340,6 +343,23 @@ public void run() { }); } + private void unregister(final CallbackContext callbackContext) { + cordova.getThreadPool().execute(new Runnable() { + public void run() { + try { + FirebaseInstanceId.getInstance().deleteInstanceId(); + String currentToken = FirebaseInstanceId.getInstance().getToken(); + if (currentToken != null) { + FirebasePlugin.sendToken(currentToken); + } + callbackContext.success(); + } catch (Exception e) { + callbackContext.error(e.getMessage()); + } + } + }); + } + private void logEvent(final CallbackContext callbackContext, final String name, final JSONObject params) throws JSONException { final Bundle bundle = new Bundle(); Iterator iter = params.keys(); diff --git a/src/ios/FirebasePlugin.h b/src/ios/FirebasePlugin.h index a88ce4522..93542737c 100755 --- a/src/ios/FirebasePlugin.h +++ b/src/ios/FirebasePlugin.h @@ -11,6 +11,7 @@ - (void)getBadgeNumber:(CDVInvokedUrlCommand*)command; - (void)subscribe:(CDVInvokedUrlCommand*)command; - (void)unsubscribe:(CDVInvokedUrlCommand*)command; +- (void)unregister:(CDVInvokedUrlCommand*)command; - (void)onNotificationOpen:(CDVInvokedUrlCommand*)command; - (void)onTokenRefresh:(CDVInvokedUrlCommand*)command; - (void)sendNotification:(NSDictionary*)userInfo; diff --git a/src/ios/FirebasePlugin.m b/src/ios/FirebasePlugin.m index d7dbf788d..9d0a7a6a1 100644 --- a/src/ios/FirebasePlugin.m +++ b/src/ios/FirebasePlugin.m @@ -144,6 +144,21 @@ - (void)unsubscribe:(CDVInvokedUrlCommand *)command { [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } +- (void)unregister:(CDVInvokedUrlCommand *)command { + [[FIRInstanceID instanceID] deleteIDWithHandler:^void(NSError *_Nullable error){ + if (error) { + NSLog(@"Unable to delete instance"); + } else { + NSString* currentToken = [[FIRInstanceID instanceID] token]; + if (currentToken != nil) { + [self sendToken:currentToken]; + } + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } + }]; +} + - (void)onNotificationOpen:(CDVInvokedUrlCommand *)command { self.notificationCallbackId = command.callbackId; diff --git a/www/firebase.js b/www/firebase.js index 4d350b8c3..cf67c8846 100644 --- a/www/firebase.js +++ b/www/firebase.js @@ -40,6 +40,10 @@ exports.unsubscribe = function(topic, success, error) { exec(success, error, "FirebasePlugin", "unsubscribe", [topic]); }; +exports.unregister = function(success, error) { + exec(success, error, "FirebasePlugin", "unregister", []); +}; + exports.logEvent = function(name, params, success, error) { exec(success, error, "FirebasePlugin", "logEvent", [name, params]); };