@@ -19,7 +19,8 @@ var _notifHandlers = {};
1919var _initialNotification = RCTPushNotificationManager &&
2020 RCTPushNotificationManager . initialNotification ;
2121
22- var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived' ;
22+ var DEVICE_NOTIF_EVENT_FOREGROUND = 'remoteNotificationReceivedForeground' ;
23+ var DEVICE_NOTIF_EVENT_BACKGROUND = 'remoteNotificationReceivedBackground' ;
2324
2425/**
2526 * Handle push notifications for your app, including permission handling and
@@ -52,19 +53,31 @@ class PushNotificationIOS {
5253 * Attaches a listener to remote notifications while the app is running in the
5354 * foreground or the background.
5455 *
55- * The handler will get be invoked with an instance of `PushNotificationIOS`
56+ * The handler will get be invoked with an instance of `PushNotificationIOS`.
57+ *
5658 */
5759 static addEventListener ( type : string , handler : Function ) {
5860 invariant (
59- type === 'notification' ,
60- 'PushNotificationIOS only supports `notification` events'
61- ) ;
62- _notifHandlers [ handler ] = RCTDeviceEventEmitter . addListener (
63- DEVICE_NOTIF_EVENT ,
64- ( notifData ) => {
65- handler ( new PushNotificationIOS ( notifData ) ) ;
66- }
61+ type === 'notificationForeground' || type === 'notificationBackground' ,
62+ 'PushNotificationIOS only supports `notificationForeground` and `notificationBackground` events'
6763 ) ;
64+
65+ if ( type === 'notificationForeground' ) {
66+ _notifHandlers [ handler ] = RCTDeviceEventEmitter . addListener (
67+ DEVICE_NOTIF_EVENT_FOREGROUND ,
68+ ( notifData ) => {
69+ handler ( new PushNotificationIOS ( notifData ) ) ;
70+ }
71+ ) ;
72+ } else {
73+ _notifHandlers [ handler ] = RCTDeviceEventEmitter . addListener (
74+ DEVICE_NOTIF_EVENT_BACKGROUND ,
75+ ( notifData ) => {
76+ handler ( new PushNotificationIOS ( notifData ) ) ;
77+ }
78+ ) ;
79+ }
80+
6881 }
6982
7083 /**
@@ -97,8 +110,8 @@ class PushNotificationIOS {
97110 */
98111 static removeEventListener ( type : string , handler : Function ) {
99112 invariant (
100- type === 'notification ' ,
101- 'PushNotificationIOS only supports `notification ` events'
113+ type === 'notificationForeground' || type === 'notificationBackground ',
114+ 'PushNotificationIOS only supports `notificationForeground` and `notificationBackground ` events'
102115 ) ;
103116 if ( ! _notifHandlers [ handler ] ) {
104117 return ;
0 commit comments