Skip to content

Commit

Permalink
sub/unsub and separated out push permissions prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Arnesson committed Jun 28, 2016
1 parent e53c1b8 commit e2ca65c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
20 changes: 18 additions & 2 deletions src/android/FirebasePlugin.java
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/ios/FirebasePlugin.h
Expand Up @@ -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
44 changes: 37 additions & 7 deletions src/ios/FirebasePlugin.m
Expand Up @@ -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];
}

Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions www/firebase.js
Expand Up @@ -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]);
};

0 comments on commit e2ca65c

Please sign in to comment.