Add observers for local notifications

Summary:
This commit adds the delegate hooks so that local notifications get
passed onto the JS and adds a new event listener type for local
notifications.

Also add functions to clear local notifications
Closes https://github.com/facebook/react-native/pull/2084

Reviewed By: svcscm

Differential Revision: D2908096

Pulled By: nicklockwood

fb-gh-sync-id: 759d299ea35abea177e72934076297d666d3ea20
shipit-source-id: 759d299ea35abea177e72934076297d666d3ea20
This commit is contained in:
slycoder
2016-02-09 05:45:23 -08:00
committed by facebook-github-bot-7
parent c6366e4dd8
commit 758d9e8304
4 changed files with 89 additions and 27 deletions

View File

@@ -21,6 +21,7 @@ var _initialNotification = RCTPushNotificationManager &&
var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
var DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived';
/**
* Handle push notifications for your app, including permission handling and
@@ -59,6 +60,11 @@ var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
* {
* [RCTPushNotificationManager didReceiveRemoteNotification:notification];
* }
* // Required for the localNotification event.
* - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
* {
* [RCTPushNotificationManager didReceiveLocalNotification:notification];
* }
* ```
*/
class PushNotificationIOS {
@@ -74,7 +80,6 @@ class PushNotificationIOS {
*
* - `alertBody` : The message displayed in the notification alert.
* - `soundName` : The sound played when the notification is fired (optional).
*
*/
static presentLocalNotification(details: Object) {
RCTPushNotificationManager.presentLocalNotification(details);
@@ -88,7 +93,7 @@ class PushNotificationIOS {
* - `fireDate` : The date and time when the system should deliver the notification.
* - `alertBody` : The message displayed in the notification alert.
* - `soundName` : The sound played when the notification is fired (optional).
*
* - `userInfo` : An optional object containing additional notification data.
*/
static scheduleLocalNotification(details: Object) {
RCTPushNotificationManager.scheduleLocalNotification(details);
@@ -115,6 +120,17 @@ class PushNotificationIOS {
RCTPushNotificationManager.getApplicationIconBadgeNumber(callback);
}
/**
* Cancel local notifications.
*
* Optionally restricts the set of canceled notifications to those
* notifications whose `userInfo` fields match the corresponding fields
* in the `userInfo` argument.
*/
static cancelLocalNotifications(userInfo: Object) {
RCTPushNotificationManager.cancelLocalNotifications(userInfo);
}
/**
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
@@ -128,8 +144,8 @@ class PushNotificationIOS {
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notification' || type === 'register' || type === 'localNotification',
'PushNotificationIOS only supports `notification`, `register` and `localNotification` events'
);
var listener;
if (type === 'notification') {
@@ -139,6 +155,13 @@ class PushNotificationIOS {
handler(new PushNotificationIOS(notifData));
}
);
} else if (type === 'localNotification') {
listener = RCTDeviceEventEmitter.addListener(
DEVICE_LOCAL_NOTIF_EVENT,
(notifData) => {
handler(new PushNotificationIOS(notifData));
}
);
} else if (type === 'register') {
listener = RCTDeviceEventEmitter.addListener(
NOTIF_REGISTER_EVENT,
@@ -220,8 +243,8 @@ class PushNotificationIOS {
*/
static removeEventListener(type: string, handler: Function) {
invariant(
type === 'notification' || type === 'register',
'PushNotificationIOS only supports `notification` and `register` events'
type === 'notification' || type === 'register' || type === 'localNotification',
'PushNotificationIOS only supports `notification`, `register` and `localNotification` events'
);
var listener = _notifHandlers.get(handler);
if (!listener) {