[fcm] More iOS event separation

This commit is contained in:
Chris Bianca 2018-02-18 16:02:31 +00:00
parent b86d51f36b
commit a244f17853
2 changed files with 82 additions and 10 deletions

View File

@ -119,9 +119,9 @@ RCT_EXPORT_MODULE()
NSDictionary *message = [self parseUNNotification:notification messageType:@"PresentNotification" openedFromTray:false]; NSDictionary *message = [self parseUNNotification:notification messageType:@"PresentNotification" openedFromTray:false];
if (isFCM || isScheduled) { if (isFCM || isScheduled) {
// If background // If app is in the background
if (RCTSharedApplication().applicationState == UIApplicationStateInactive) { if (RCTSharedApplication().applicationState == UIApplicationStateInactive) {
// display notification // display the notification
options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound;
// notification_displayed // notification_displayed
event = NOTIFICATIONS_NOTIFICATION_DISPLAYED; event = NOTIFICATIONS_NOTIFICATION_DISPLAYED;
@ -132,7 +132,8 @@ RCT_EXPORT_MODULE()
event = NOTIFICATIONS_NOTIFICATION_RECEIVED; event = NOTIFICATIONS_NOTIFICATION_RECEIVED;
} }
} else { } else {
// display notification // Triggered by `notifications().displayNotification(notification)`
// Display the notification
options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound;
// no event // no event
} }
@ -482,7 +483,7 @@ RCT_EXPORT_METHOD(completeRemoteNotification: (NSString*) messageId
} }
- (NSArray<NSString *> *)supportedEvents { - (NSArray<NSString *> *)supportedEvents {
return @[MESSAGING_MESSAGE_RECEIVED, MESSAGING_TOKEN_REFRESHED]; return @[MESSAGING_MESSAGE_RECEIVED, MESSAGING_TOKEN_REFRESHED, NOTIFICATIONS_NOTIFICATION_CLICKED, NOTIFICATIONS_NOTIFICATION_DISPLAYED, NOTIFICATIONS_NOTIFICATION_RECEIVED];
} }
+ (BOOL)requiresMainQueueSetup + (BOOL)requiresMainQueueSetup

View File

@ -32,7 +32,11 @@ export type Schedule = {
repeatInterval?: 'minute' | 'hour' | 'day' | 'week', repeatInterval?: 'minute' | 'hour' | 'day' | 'week',
}; };
const NATIVE_EVENTS = ['notifications_notification_received']; const NATIVE_EVENTS = [
'notifications_notification_clicked',
'notifications_notification_displayed',
'notifications_notification_received',
];
export const MODULE_NAME = 'RNFirebaseNotifications'; export const MODULE_NAME = 'RNFirebaseNotifications';
export const NAMESPACE = 'notifications'; export const NAMESPACE = 'notifications';
@ -65,7 +69,25 @@ export default class Notifications extends ModuleBase {
SharedEventEmitter.addListener( SharedEventEmitter.addListener(
// sub to internal native event - this fans out to // sub to internal native event - this fans out to
// public event name: onMessage // public event name: onNotificationClicked
'notifications_notification_clicked',
(notification: Notification) => {
SharedEventEmitter.emit('onNotificationClicked', notification);
}
);
SharedEventEmitter.addListener(
// sub to internal native event - this fans out to
// public event name: onNotificationDisplayed
'notifications_notification_displayed',
(notification: Notification) => {
SharedEventEmitter.emit('onNotificationDisplayed', notification);
}
);
SharedEventEmitter.addListener(
// sub to internal native event - this fans out to
// public event name: onNotification
'notifications_notification_received', 'notifications_notification_received',
(notification: Notification) => { (notification: Notification) => {
SharedEventEmitter.emit('onNotification', notification); SharedEventEmitter.emit('onNotification', notification);
@ -73,12 +95,16 @@ export default class Notifications extends ModuleBase {
); );
} }
/**
* Cancel all notifications
* @returns {*}
*/
cancelAllNotifications(): Promise<void> { cancelAllNotifications(): Promise<void> {
return getNativeModule(this).cancelAllNotifications(); return getNativeModule(this).cancelAllNotifications();
} }
/** /**
* Cancel a local notification by id. * Cancel a notification by id.
* @param id * @param id
* @returns {*} * @returns {*}
*/ */
@ -90,7 +116,7 @@ export default class Notifications extends ModuleBase {
} }
/** /**
* Display a local notification * Display a notification
* @param notification * @param notification
* @returns {*} * @returns {*}
*/ */
@ -131,7 +157,6 @@ export default class Notifications extends ModuleBase {
); );
} }
// TODO: iOS finish
getLogger(this).info('Creating onNotification listener'); getLogger(this).info('Creating onNotification listener');
SharedEventEmitter.addListener('onNotification', listener); SharedEventEmitter.addListener('onNotification', listener);
@ -141,6 +166,52 @@ export default class Notifications extends ModuleBase {
}; };
} }
onNotificationClicked(
nextOrObserver: OnNotification | OnNotificationObserver
): () => any {
let listener;
if (isFunction(nextOrObserver)) {
listener = nextOrObserver;
} else if (isObject(nextOrObserver) && isFunction(nextOrObserver.next)) {
listener = nextOrObserver.next;
} else {
throw new Error(
'Notifications.onNotificationClicked failed: First argument must be a function or observer object with a `next` function.'
);
}
getLogger(this).info('Creating onNotificationClicked listener');
SharedEventEmitter.addListener('onNotificationClicked', listener);
return () => {
getLogger(this).info('Removing onNotificationClicked listener');
SharedEventEmitter.removeListener('onNotificationClicked', listener);
};
}
onNotificationDisplayed(
nextOrObserver: OnNotification | OnNotificationObserver
): () => any {
let listener;
if (isFunction(nextOrObserver)) {
listener = nextOrObserver;
} else if (isObject(nextOrObserver) && isFunction(nextOrObserver.next)) {
listener = nextOrObserver.next;
} else {
throw new Error(
'Notifications.onNotificationDisplayed failed: First argument must be a function or observer object with a `next` function.'
);
}
getLogger(this).info('Creating onNotificationDisplayed listener');
SharedEventEmitter.addListener('onNotificationDisplayed', listener);
return () => {
getLogger(this).info('Removing onNotificationDisplayed listener');
SharedEventEmitter.removeListener('onNotificationDisplayed', listener);
};
}
/** /**
* Remove all delivered notifications. * Remove all delivered notifications.
* @returns {*} * @returns {*}
@ -162,7 +233,7 @@ export default class Notifications extends ModuleBase {
} }
/** /**
* * Schedule a notification
* @param notification * @param notification
* @returns {*} * @returns {*}
*/ */