Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Notification flow:

1. App is in foreground:
1. User receives the notification data in the JavaScript callback without any notification on the device itself (this is the normal behaviour of push notifications, it is up to you, the developer, to notify the user)
2. Alternatively, you may call enableForegroundNotifications() during app startup, and then user will receive notification messages in the device notification bar, even when the app is in the foreground.
2. App is in background:
1. User receives the notification message in its device notification bar
2. User taps the notification and the app opens
Expand All @@ -51,6 +52,13 @@ Notification icon on Android:

[Changing notification icon](NOTIFICATIONS.md#changing-notification-icon)

## enableForegroundNotifications

Turn on foreground notifications:
```
window.FirebasePlugin.enableForegroundNotifications();
```

## grantPermission (iOS only)

Grant permission to receive push notifications (will trigger prompt):
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ xmlns:android="http://schemas.android.com/apk/res/android">
<source-file src="src/android/FirebasePluginMessagingService.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/FirebasePluginMessageReceiver.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/FirebasePluginMessageReceiverManager.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/colors.xml" target-dir="res/values" />
<resource-file src="src/android/colors.xml" target="res/values/colors.xml" />

<framework src="src/android/build.gradle" custom="true" type="gradleReference" />
<framework src="com.google.android.gms:play-services-tagmanager:+" />
Expand Down
19 changes: 16 additions & 3 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class FirebasePlugin extends CordovaPlugin {
protected static final String KEY = "badge";

private static boolean inBackground = true;
private static boolean foregroundEnabled = false;
private static ArrayList<Bundle> notificationStack = null;
private static CallbackContext notificationCallbackContext;
private static CallbackContext tokenRefreshCallbackContext;
Expand Down Expand Up @@ -122,6 +123,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
} else if (action.equals("onNotificationOpen")) {
this.onNotificationOpen(callbackContext);
return true;
} else if (action.equals("enableForegroundNotifications")) {
this.enableForegroundNotifications(callbackContext);
return true;
} else if (action.equals("onTokenRefresh")) {
this.onTokenRefresh(callbackContext);
return true;
Expand Down Expand Up @@ -241,6 +245,11 @@ private void onNotificationOpen(final CallbackContext callbackContext) {
}
}

private void enableForegroundNotifications(final CallbackContext callbackContext) {
FirebasePlugin.foregroundEnabled = true;
callbackContext.success();
}

private void onTokenRefresh(final CallbackContext callbackContext) {
FirebasePlugin.tokenRefreshCallbackContext = callbackContext;

Expand Down Expand Up @@ -306,6 +315,10 @@ public static boolean inBackground() {
return FirebasePlugin.inBackground;
}

public static boolean foregroundEnabled() {
return FirebasePlugin.foregroundEnabled;
}

public static boolean hasNotificationsCallback() {
return FirebasePlugin.notificationCallbackContext != null;
}
Expand Down Expand Up @@ -747,7 +760,7 @@ public void onVerificationCompleted(PhoneAuthCredential credential) {
try {
String verificationId = null;
String code = null;

Field[] fields = credential.getClass().getDeclaredFields();
for (Field field : fields) {
Class type = field.getType();
Expand Down Expand Up @@ -814,7 +827,7 @@ public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingTo
callbackContext.sendPluginResult(pluginresult);
}
};

PhoneAuthProvider.getInstance().verifyPhoneNumber(number, // Phone number to verify
timeOutDuration, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
Expand All @@ -827,7 +840,7 @@ public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingTo
}
});
}

private static String getPrivateField(PhoneAuthCredential credential, Field field) {
try {
field.setAccessible(true);
Expand Down
3 changes: 1 addition & 2 deletions src/android/FirebasePluginMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "Notification Message Sound: " + sound);
Log.d(TAG, "Notification Message Lights: " + lights);

// TODO: Add option to developer to configure if show notification when app on foreground
if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()) && (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title));
boolean showNotification = (FirebasePlugin.inBackground() || FirebasePlugin.foregroundEnabled() || !FirebasePlugin.hasNotificationsCallback()) && (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title));
sendNotification(id, title, text, data, showNotification, sound, lights);
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/ios/AppDelegate+FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ - (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithO

// get GoogleService-Info.plist file path
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];

// if file is successfully found, use it
if(filePath){
NSLog(@"GoogleService-Info.plist found, setup: [FIRApp configureWithOptions]");
// create firebase configure options passing .plist as content
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];

// configure FIRApp with options
[FIRApp configureWithOptions:options];
}

// no .plist found, try default App
if (![FIRApp defaultApp] && !filePath) {
NSLog(@"GoogleService-Info.plist NOT FOUND, setup: [FIRApp defaultApp]");
Expand Down Expand Up @@ -180,7 +180,13 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
// Print full message.
NSLog(@"%@", mutableUserInfo);

completionHandler(UNNotificationPresentationOptionAlert);
if ([self.applicationInBackground isEqualToNumber: @(YES)]
|| [[FirebasePlugin.firebasePlugin foregroundEnabled] isEqualToNumber: @(YES)]) {
completionHandler(UNNotificationPresentationOptionAlert);
} else {
completionHandler(UNNotificationPresentationOptionNone);
}

[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
}

Expand Down
2 changes: 2 additions & 0 deletions src/ios/FirebasePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
- (void)unregister:(CDVInvokedUrlCommand*)command;
- (void)onNotificationOpen:(CDVInvokedUrlCommand*)command;
- (void)enableForegroundNotifications:(CDVInvokedUrlCommand*)command;
- (void)onTokenRefresh:(CDVInvokedUrlCommand*)command;
- (void)sendNotification:(NSDictionary*)userInfo;
- (void)sendToken:(NSString*)token;
Expand All @@ -38,5 +39,6 @@
@property (nonatomic, copy) NSString *tokenRefreshCallbackId;
@property (nonatomic, retain) NSMutableArray *notificationStack;
@property (nonatomic, readwrite) NSMutableDictionary* traces;
@property (nonatomic, strong) NSNumber *foregroundEnabled;

@end
18 changes: 18 additions & 0 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "Firebase.h"
#import <Fabric/Fabric.h>
#import <Crashlytics/Crashlytics.h>
#import <objc/runtime.h>
@import FirebaseInstanceID;
@import FirebaseMessaging;
@import FirebaseAnalytics;
Expand All @@ -19,6 +20,8 @@
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif

#define kForegoundEnabledKey @"foregroundEnabled"

@implementation FirebasePlugin

@synthesize notificationCallbackId;
Expand All @@ -38,6 +41,14 @@ - (void)pluginInitialize {
firebasePlugin = self;
}

- (void)setForegroundEnabled:(NSNumber *)foregroundEnabled {
objc_setAssociatedObject(self, kForegoundEnabledKey, foregroundEnabled, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)foregroundEnabled {
return objc_getAssociatedObject(self, kForegoundEnabledKey);
}

- (void)getId:(CDVInvokedUrlCommand *)command {
__block CDVPluginResult *pluginResult;

Expand Down Expand Up @@ -229,6 +240,13 @@ - (void)onNotificationOpen:(CDVInvokedUrlCommand *)command {
}
}

- (void)enableForegroundNotifications:(CDVInvokedUrlCommand *)command {
self.foregroundEnabled = @(YES);

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)onTokenRefresh:(CDVInvokedUrlCommand *)command {
self.tokenRefreshCallbackId = command.callbackId;
NSString* currentToken = [[FIRInstanceID instanceID] token];
Expand Down
8 changes: 7 additions & 1 deletion www/firebase-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ exports.getId = function (success, error) {

exports.onNotificationOpen = function (success, error) {};

exports.enableForegroundNotifications = function (success, error) {
if (typeof success === 'function') {
success();
}
};

exports.onTokenRefresh = function (success, error) {};

exports.grantPermission = function (success, error) {
Expand Down Expand Up @@ -156,4 +162,4 @@ exports.clearAllNotifications = function (success, error) {
if (typeof success === 'function') {
success();
}
};
};
6 changes: 5 additions & 1 deletion www/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ exports.onNotificationOpen = function (success, error) {
exec(success, error, "FirebasePlugin", "onNotificationOpen", []);
};

exports.enableForegroundNotifications = function (success, error) {
exec(success, error, "FirebasePlugin", "enableForegroundNotifications", []);
}

exports.onTokenRefresh = function (success, error) {
exec(success, error, "FirebasePlugin", "onTokenRefresh", []);
};
Expand Down Expand Up @@ -166,4 +170,4 @@ exports.verifyPhoneNumber = function (number, timeOutDuration, success, error) {

exports.clearAllNotifications = function (success, error) {
exec(success, error, "FirebasePlugin", "clearAllNotifications", []);
};
};