[notifications] Add jsInitialised method to improve getInitialNotification

This commit is contained in:
Chris Bianca 2018-03-30 10:31:06 +01:00
parent 06c06b764f
commit 801adabb27
2 changed files with 31 additions and 21 deletions

View File

@ -22,6 +22,7 @@ static RNFirebaseNotifications *theRNFirebaseNotifications = nil;
// PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side // PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side
// static NSMutableArray *pendingEvents = nil; // static NSMutableArray *pendingEvents = nil;
static NSDictionary *initialNotification = nil; static NSDictionary *initialNotification = nil;
static bool jsReady = FALSE;
+ (nonnull instancetype)instance { + (nonnull instancetype)instance {
return theRNFirebaseNotifications; return theRNFirebaseNotifications;
@ -266,28 +267,24 @@ RCT_EXPORT_METHOD(getBadge: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromise
} }
RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
if ([self isIOS89]) { // Check if we've cached an initial notification as this will contain the accurate action
// With iOS 8/9 we rely on the launch options if (initialNotification) {
UILocalNotification *localNotification = [self bridge].launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; resolve(initialNotification);
NSDictionary *notification; } else if (self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
if (localNotification) { UILocalNotification *localNotification = self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
notification = [self parseUILocalNotification:localNotification]; resolve(@{
} else { @"action": UNNotificationDefaultActionIdentifier,
@"notification": [self parseUILocalNotification:localNotification]
});
} else if (self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSDictionary *remoteNotification = [self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; NSDictionary *remoteNotification = [self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) { resolve(@{
notification = [self parseUserInfo:remoteNotification]; @"action": UNNotificationDefaultActionIdentifier,
} @"notification": [self parseUserInfo:remoteNotification]
} });
if (notification) {
resolve(@{@"action": UNNotificationDefaultActionIdentifier, @"notification": notification});
} else { } else {
resolve(nil); resolve(nil);
} }
} else {
// With iOS 10+ we are able to intercept the didReceiveNotificationResponse message
// to get both the notification and the action
resolve(initialNotification);
}
} }
RCT_EXPORT_METHOD(getScheduledNotifications:(RCTPromiseResolveBlock)resolve RCT_EXPORT_METHOD(getScheduledNotifications:(RCTPromiseResolveBlock)resolve
@ -374,15 +371,22 @@ RCT_EXPORT_METHOD(setBadge:(NSInteger) number
}); });
} }
RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
jsReady = TRUE;
resolve(nil);
}
// Because of the time delay between the app starting and the bridge being initialised // Because of the time delay between the app starting and the bridge being initialised
// we create a temporary instance of RNFirebaseNotifications. // we create a temporary instance of RNFirebaseNotifications.
// With this temporary instance, we cache any events to be sent as soon as the bridge is set on the module // With this temporary instance, we cache any events to be sent as soon as the bridge is set on the module
- (void)sendJSEvent:(RCTEventEmitter *)emitter name:(NSString *)name body:(id)body { - (void)sendJSEvent:(RCTEventEmitter *)emitter name:(NSString *)name body:(id)body {
if (emitter.bridge) { if (emitter.bridge && jsReady) {
[RNFirebaseUtil sendJSEvent:emitter name:name body:body]; [RNFirebaseUtil sendJSEvent:emitter name:name body:body];
} else { } else {
if ([name isEqualToString:NOTIFICATIONS_NOTIFICATION_OPENED] && !initialNotification) { if ([name isEqualToString:NOTIFICATIONS_NOTIFICATION_OPENED] && !initialNotification) {
initialNotification = body; initialNotification = body;
} else if ([name isEqualToString:NOTIFICATIONS_NOTIFICATION_OPENED]) {
NSLog(@"Multiple notification open events received before the JS Notifications module has been initialised");
} }
// PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side // PRE-BRIDGE-EVENTS: Consider enabling this to allow events built up before the bridge is built to be sent to the JS side
// [pendingEvents addObject:@{@"name":name, @"body":body}]; // [pendingEvents addObject:@{@"name":name, @"body":body}];

View File

@ -2,6 +2,7 @@
* @flow * @flow
* Notifications representation wrapper * Notifications representation wrapper
*/ */
import { Platform } from 'react-native';
import { SharedEventEmitter } from '../../utils/events'; import { SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log'; import { getLogger } from '../../utils/log';
import ModuleBase from '../../utils/ModuleBase'; import ModuleBase from '../../utils/ModuleBase';
@ -119,6 +120,11 @@ export default class Notifications extends ModuleBase {
); );
} }
); );
// Tell the native module that we're ready to receive events
if (Platform.OS === 'ios') {
getNativeModule(this).jsInitialised();
}
} }
get android(): AndroidNotifications { get android(): AndroidNotifications {