/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule PushNotificationIOS
* @flow
*/
'use strict';
const NativeEventEmitter = require('NativeEventEmitter');
const RCTPushNotificationManager = require('NativeModules').PushNotificationManager;
const invariant = require('fbjs/lib/invariant');
const PushNotificationEmitter = new NativeEventEmitter(RCTPushNotificationManager);
const _notifHandlers = new Map();
const DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
const NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
const NOTIF_REGISTRATION_ERROR_EVENT = 'remoteNotificationRegistrationError';
const DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived';
export type ContentAvailable = 1 | null | void;
export type FetchResult = {
NewData: string,
NoData: string,
ResultFailed: string,
};
/**
* An event emitted by PushNotificationIOS.
*/
export type PushNotificationEventName = $Enum<{
/**
* Fired when a remote notification is received. The handler will be invoked
* with an instance of `PushNotificationIOS`.
*/
notification: string,
/**
* Fired when a local notification is received. The handler will be invoked
* with an instance of `PushNotificationIOS`.
*/
localNotification: string,
/**
* Fired when the user registers for remote notifications. The handler will be
* invoked with a hex string representing the deviceToken.
*/
register: string,
/**
* Fired when the user fails to register for remote notifications. Typically
* occurs when APNS is having issues, or the device is a simulator. The
* handler will be invoked with {message: string, code: number, details: any}.
*/
registrationError: string,
}>;
/**
*
*
Projects with Native Code Only
*
* This section only applies to projects made with react-native init
* or to those made with Create React Native App which have since ejected. For
* more information about ejecting, please see
* the guide on
* the Create React Native App repository.
*
*
*
* Handle push notifications for your app, including permission handling and
* icon badge number.
*
* To get up and running, [configure your notifications with Apple](https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW6)
* and your server-side system.
*
* [Manually link](docs/linking-libraries-ios.html#manual-linking) the PushNotificationIOS library
*
* - Add the following to your Project: `node_modules/react-native/Libraries/PushNotificationIOS/RCTPushNotification.xcodeproj`
* - Add the following to `Link Binary With Libraries`: `libRCTPushNotification.a`
*
* Finally, to enable support for `notification` and `register` events you need to augment your AppDelegate.
*
* At the top of your `AppDelegate.m`:
*
* `#import `
*
* And then in your AppDelegate implementation add the following:
*
* ```
* // Required to register for notifications
* - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
* {
* [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
* }
* // Required for the register event.
* - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
* {
* [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
* }
* // Required for the notification event. You must call the completion handler after handling the remote notification.
* - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
* fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
* {
* [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
* }
* // Required for the registrationError event.
* - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
* {
* [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
* }
* // Required for the localNotification event.
* - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
* {
* [RCTPushNotificationManager didReceiveLocalNotification:notification];
* }
* ```
*/
class PushNotificationIOS {
_data: Object;
_alert: string | Object;
_sound: string;
_category: string;
_contentAvailable: ContentAvailable;
_badgeCount: number;
_notificationId: string;
_isRemote: boolean;
_remoteNotificationCompleteCallbackCalled: boolean;
static FetchResult: FetchResult = {
NewData: 'UIBackgroundFetchResultNewData',
NoData: 'UIBackgroundFetchResultNoData',
ResultFailed: 'UIBackgroundFetchResultFailed',
};
/**
* Schedules the localNotification for immediate presentation.
*
* details is an object containing:
*
* - `alertBody` : The message displayed in the notification alert.
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
* - `soundName` : The sound played when the notification is fired (optional).
* - `isSilent` : If true, the notification will appear without sound (optional).
* - `category` : The category of this notification, required for actionable notifications (optional).
* - `userInfo` : An optional object containing additional notification data.
* - `applicationIconBadgeNumber` (optional) : The number to display as the app's icon badge. The default value of this property is 0, which means that no badge is displayed.
*/
static presentLocalNotification(details: Object) {
RCTPushNotificationManager.presentLocalNotification(details);
}
/**
* Schedules the localNotification for future presentation.
*
* details is an object containing:
*
* - `fireDate` : The date and time when the system should deliver the notification.
* - `alertTitle` : The text displayed as the title of the notification alert.
* - `alertBody` : The message displayed in the notification alert.
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
* - `soundName` : The sound played when the notification is fired (optional).
* - `isSilent` : If true, the notification will appear without sound (optional).
* - `category` : The category of this notification, required for actionable notifications (optional).
* - `userInfo` : An optional object containing additional notification data.
* - `applicationIconBadgeNumber` (optional) : The number to display as the app's icon badge. Setting the number to 0 removes the icon badge.
* - `repeatInterval` : The interval to repeat as a string. Possible values: `minute`, `hour`, `day`, `week`, `month`, `year`.
*/
static scheduleLocalNotification(details: Object) {
RCTPushNotificationManager.scheduleLocalNotification(details);
}
/**
* Cancels all scheduled localNotifications
*/
static cancelAllLocalNotifications() {
RCTPushNotificationManager.cancelAllLocalNotifications();
}
/**
* Remove all delivered notifications from Notification Center
*/
static removeAllDeliveredNotifications(): void {
RCTPushNotificationManager.removeAllDeliveredNotifications();
}
/**
* Provides you with a list of the app’s notifications that are still displayed in Notification Center
*
* @param callback Function which receive an array of delivered notifications
*
* A delivered notification is an object containing:
*
* - `identifier` : The identifier of this notification.
* - `title` : The title of this notification.
* - `body` : The body of this notification.
* - `category` : The category of this notification, if has one.
* - `userInfo` : An optional object containing additional notification data.
* - `thread-id` : The thread identifier of this notification, if has one.
*/
static getDeliveredNotifications(callback: (notifications: Array