From e2ca65c76a072da7ea87096582b6716ff15b25d8 Mon Sep 17 00:00:00 2001 From: Robert Arnesson Date: Tue, 28 Jun 2016 17:27:01 -0400 Subject: [PATCH] sub/unsub and separated out push permissions prompt --- README.md | 21 ++++++++++++++++ src/android/FirebasePlugin.java | 20 +++++++++++++-- src/ios/FirebasePlugin.h | 3 +++ src/ios/FirebasePlugin.m | 44 +++++++++++++++++++++++++++------ www/firebase.js | 12 +++++++++ 5 files changed, 91 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2cd5b7d9b..0d3ad9e40 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,27 @@ window.FirebasePlugin.getInstanceId(function(token) { }); ``` +### grantPermission (iOS only) + +Grant permission to recieve push notifications (will trigger prompt): +``` +window.FirebasePlugin.grantPermission(); +``` + +### subscribe + +Subscribe to a topic: +``` +window.FirebasePlugin.subscribe("/topics/example"); +``` + +### unsubscribe + +Unsubscribe from a topic: +``` +window.FirebasePlugin.unsubscribe("/topics/example"); +``` + ### logEvent Log an event using Analytics: diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index 661a6fc78..e1915dc59 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -35,6 +35,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo if (action.equals("getInstanceId")) { this.getInstanceId(callbackContext); return true; + } else if (action.equals("subscribe")) { + this.subscribe(callbackContext, args.getString(0)); + return true; + } else if (action.equals("unsubscribe")) { + this.unsubscribe(callbackContext, args.getString(0)); + return true; } else if (action.equals("logEvent")) { this.logEvent(callbackContext, args.getString(0), args.getString(1)); return true; @@ -43,8 +49,18 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } private void getInstanceId(CallbackContext callbackContext) { - String refreshedToken = FirebaseInstanceId.getInstance().getToken(); - callbackContext.success(refreshedToken); + String token = FirebaseInstanceId.getInstance().getToken(); + callbackContext.success(token); + } + + private void subscribe(CallbackContext callbackContext, String topic) { + FirebaseInstanceId.getInstance().subscribeToTopic(topic); + callbackContext.success(); + } + + private void unsubscribe(CallbackContext callbackContext, String topic) { + FirebaseInstanceId.getInstance().unsubscribeFromTopic(topic); + callbackContext.success(); } private void logEvent(CallbackContext callbackContext, String key, String value) { diff --git a/src/ios/FirebasePlugin.h b/src/ios/FirebasePlugin.h index 706fc253c..b4cf61e8b 100755 --- a/src/ios/FirebasePlugin.h +++ b/src/ios/FirebasePlugin.h @@ -3,5 +3,8 @@ @interface FirebasePlugin : CDVPlugin - (void)getInstanceId:(CDVInvokedUrlCommand*)command; +- (void)grantPermission:(CDVInvokedUrlCommand*)command; +- (void)subscribe:(CDVInvokedUrlCommand*)command; +- (void)unsubscribe:(CDVInvokedUrlCommand*)command; - (void)logEvent:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/FirebasePlugin.m b/src/ios/FirebasePlugin.m index 2e5b457e1..c29712477 100644 --- a/src/ios/FirebasePlugin.m +++ b/src/ios/FirebasePlugin.m @@ -28,14 +28,9 @@ - (void)pluginInitialize { name:UIApplicationDidEnterBackgroundNotification object:nil]; } -- (void) applicationDidFinishLaunching:(NSNotification *) notification { - UIUserNotificationType allNotificationTypes = - (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); - UIUserNotificationSettings *settings = - [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; - [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; +- (void) applicationDidFinishLaunching:(NSNotification *) notification { [[UIApplication sharedApplication] registerForRemoteNotifications]; - + [FIRApp configure]; } @@ -61,6 +56,41 @@ - (void)getInstanceId:(CDVInvokedUrlCommand *)command { [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } +- (void)grantPermission:(CDVInvokedUrlCommand *)command { + CDVPluginResult *pluginResult; + + UIUserNotificationType allNotificationTypes = + (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); + UIUserNotificationSettings *settings = + [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; + [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; + + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)subscribe:(CDVInvokedUrlCommand *)command { + CDVPluginResult *pluginResult; + + NSString* topic = [command.arguments objectAtIndex:0]; + + [[FIRMessaging messaging] subscribeToTopic: topic]; + + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)unsubscribe:(CDVInvokedUrlCommand *)command { + CDVPluginResult *pluginResult; + + NSString* topic = [command.arguments objectAtIndex:0]; + + [[FIRMessaging messaging] unsubscribeFromTopic: topic]; + + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + - (void)logEvent:(CDVInvokedUrlCommand *)command { [self.commandDelegate runInBackground:^{ CDVPluginResult *pluginResult; diff --git a/www/firebase.js b/www/firebase.js index bfd5010f4..7766be2c7 100644 --- a/www/firebase.js +++ b/www/firebase.js @@ -4,6 +4,18 @@ exports.getInstanceId = function(success, error) { exec(success, error, "FirebasePlugin", "getInstanceId", []); }; +exports.grantPermission = function(success, error) { + exec(success, error, "FirebasePlugin", "grantPermission", []); +}; + +exports.unsubscribe = function(topic, success, error) { + exec(success, error, "FirebasePlugin", "unsubscribe", [topic]); +}; + +exports.unsubscribe = function(topic, success, error) { + exec(success, error, "FirebasePlugin", "unsubscribe", [topic]); +}; + exports.logEvent = function(key, value, success, error) { exec(success, error, "FirebasePlugin", "logEvent", [key, value]); };