From 8ac55ee92b1cf09fc8e2d2c2974a92b46ed4e68d Mon Sep 17 00:00:00 2001 From: Jonathan Stanton Date: Tue, 14 Jun 2016 06:02:36 -0700 Subject: [PATCH] Instantiate local notifications different than remote notifications Summary: To use `PushNotificationIOS` for local notifications in the same way as we use them for remote notifications. Today remote notifications are being constructed correctly but local notifications are not. Related issue: https://github.com/facebook/react-native/issues/8014 When ObjC receives local notifications it does not serialize the `UILocalNotification` the same way as we do for remote notifications. And when we go to construct a `PushNotificationIOS` javascript instance it works for remote notifications but not for local notifications. - Some of remote notification's data is wrapped in an `aps` dict (https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1). - All of local notifications are being received and passed to javascript as a flat dictionary: (https://github.com/facebook/react-native/blob/maste Closes https://github.com/facebook/react-native/pull/8029 Differential Revision: D3417260 Pulled By: javache fbshipit-source-id: c130aa39e89ffbbd8b1243b6dacbf95bb591b4da --- .../PushNotificationIOS.js | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index a0d54c217..f6f5ffc90 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -173,6 +173,7 @@ class PushNotificationIOS { listener = PushNotificationEmitter.addListener( DEVICE_NOTIF_EVENT, (notifData) => { + notifData.remote = true; handler(new PushNotificationIOS(notifData)); } ); @@ -180,6 +181,7 @@ class PushNotificationIOS { listener = PushNotificationEmitter.addListener( DEVICE_LOCAL_NOTIF_EVENT, (notifData) => { + notifData.remote = false; handler(new PushNotificationIOS(notifData)); } ); @@ -297,7 +299,7 @@ class PushNotificationIOS { _initialNotification = null; return initialNotification; } - + /** * If the app launch was triggered by a push notification, * it will give the notification object, otherwise it will give `null` @@ -316,19 +318,26 @@ class PushNotificationIOS { constructor(nativeNotif: Object) { this._data = {}; - // Extract data from Apple's `aps` dict as specified here: - // https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html - - Object.keys(nativeNotif).forEach((notifKey) => { - var notifVal = nativeNotif[notifKey]; - if (notifKey === 'aps') { - this._alert = notifVal.alert; - this._sound = notifVal.sound; - this._badgeCount = notifVal.badge; - } else { - this._data[notifKey] = notifVal; - } - }); + if (nativeNotif.remote) { + // Extract data from Apple's `aps` dict as defined: + // https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html + Object.keys(nativeNotif).forEach((notifKey) => { + var notifVal = nativeNotif[notifKey]; + if (notifKey === 'aps') { + this._alert = notifVal.alert; + this._sound = notifVal.sound; + this._badgeCount = notifVal.badge; + } else { + this._data[notifKey] = notifVal; + } + }); + } else { + // Local notifications aren't being sent down with `aps` dict. + this._badgeCount = nativeNotif.applicationIconBadgeNumber; + this._sound = nativeNotif.soundName; + this._alert = nativeNotif.alertBody; + this._data = nativeNotif.userInfo; + } } /**