diff --git a/ios/RNFirebase/messaging/RNFirebaseMessaging.m b/ios/RNFirebase/messaging/RNFirebaseMessaging.m index 8198a8b7..7e916620 100644 --- a/ios/RNFirebase/messaging/RNFirebaseMessaging.m +++ b/ios/RNFirebase/messaging/RNFirebaseMessaging.m @@ -119,9 +119,9 @@ RCT_EXPORT_MODULE() NSDictionary *message = [self parseUNNotification:notification messageType:@"PresentNotification" openedFromTray:false]; if (isFCM || isScheduled) { - // If background + // If app is in the background if (RCTSharedApplication().applicationState == UIApplicationStateInactive) { - // display notification + // display the notification options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; // notification_displayed event = NOTIFICATIONS_NOTIFICATION_DISPLAYED; @@ -132,7 +132,8 @@ RCT_EXPORT_MODULE() event = NOTIFICATIONS_NOTIFICATION_RECEIVED; } } else { - // display notification + // Triggered by `notifications().displayNotification(notification)` + // Display the notification options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; // no event } @@ -482,7 +483,7 @@ RCT_EXPORT_METHOD(completeRemoteNotification: (NSString*) messageId } - (NSArray *)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 diff --git a/lib/modules/notifications/index.js b/lib/modules/notifications/index.js index 17857fbd..57aed391 100644 --- a/lib/modules/notifications/index.js +++ b/lib/modules/notifications/index.js @@ -32,7 +32,11 @@ export type Schedule = { 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 NAMESPACE = 'notifications'; @@ -65,7 +69,25 @@ export default class Notifications extends ModuleBase { SharedEventEmitter.addListener( // 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', (notification: Notification) => { SharedEventEmitter.emit('onNotification', notification); @@ -73,12 +95,16 @@ export default class Notifications extends ModuleBase { ); } + /** + * Cancel all notifications + * @returns {*} + */ cancelAllNotifications(): Promise { return getNativeModule(this).cancelAllNotifications(); } /** - * Cancel a local notification by id. + * Cancel a notification by id. * @param id * @returns {*} */ @@ -90,7 +116,7 @@ export default class Notifications extends ModuleBase { } /** - * Display a local notification + * Display a notification * @param notification * @returns {*} */ @@ -131,7 +157,6 @@ export default class Notifications extends ModuleBase { ); } - // TODO: iOS finish getLogger(this).info('Creating 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. * @returns {*} @@ -162,7 +233,7 @@ export default class Notifications extends ModuleBase { } /** - * + * Schedule a notification * @param notification * @returns {*} */