2015-03-12 19:51:44 +00:00
|
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
|
* 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.
|
2015-03-12 19:51:44 +00:00
|
|
|
|
*
|
|
|
|
|
* @providesModule PushNotificationIOS
|
2015-03-24 16:26:16 +00:00
|
|
|
|
* @flow
|
2015-03-12 19:51:44 +00:00
|
|
|
|
*/
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
|
const NativeEventEmitter = require('NativeEventEmitter');
|
|
|
|
|
const RCTPushNotificationManager = require('NativeModules').PushNotificationManager;
|
|
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-03-17 02:01:28 +00:00
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
|
const PushNotificationEmitter = new NativeEventEmitter(RCTPushNotificationManager);
|
|
|
|
|
|
|
|
|
|
const _notifHandlers = new Map();
|
|
|
|
|
|
|
|
|
|
const DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
|
|
|
|
|
const NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
|
2016-09-06 18:01:31 +00:00
|
|
|
|
const NOTIF_REGISTRATION_ERROR_EVENT = 'remoteNotificationRegistrationError';
|
2016-05-27 17:14:12 +00:00
|
|
|
|
const DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived';
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
2017-06-19 23:37:31 +00:00
|
|
|
|
export type ContentAvailable = 1 | null | void;
|
|
|
|
|
|
2016-10-27 04:35:33 +00:00
|
|
|
|
export type FetchResult = {
|
|
|
|
|
NewData: string,
|
|
|
|
|
NoData: string,
|
|
|
|
|
ResultFailed: string,
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-06 18:01:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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,
|
|
|
|
|
}>;
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2017-04-26 14:09:36 +00:00
|
|
|
|
* <div class="banner-crna-ejected">
|
|
|
|
|
* <h3>Projects with Native Code Only</h3>
|
|
|
|
|
* <p>
|
|
|
|
|
* This section only applies to projects made with <code>react-native init</code>
|
|
|
|
|
* or to those made with Create React Native App which have since ejected. For
|
|
|
|
|
* more information about ejecting, please see
|
|
|
|
|
* the <a href="https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md" target="_blank">guide</a> on
|
|
|
|
|
* the Create React Native App repository.
|
|
|
|
|
* </p>
|
|
|
|
|
* </div>
|
|
|
|
|
*
|
2015-03-26 16:28:03 +00:00
|
|
|
|
* Handle push notifications for your app, including permission handling and
|
|
|
|
|
* icon badge number.
|
|
|
|
|
*
|
2015-09-30 22:36:09 +00:00
|
|
|
|
* 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)
|
2017-04-26 14:09:36 +00:00
|
|
|
|
* and your server-side system.
|
2015-10-14 16:06:37 +00:00
|
|
|
|
*
|
2016-02-11 14:16:34 +00:00
|
|
|
|
* [Manually link](docs/linking-libraries-ios.html#manual-linking) the PushNotificationIOS library
|
2016-01-06 18:19:09 +00:00
|
|
|
|
*
|
2016-06-09 23:26:00 +00:00
|
|
|
|
* - Add the following to your Project: `node_modules/react-native/Libraries/PushNotificationIOS/RCTPushNotification.xcodeproj`
|
|
|
|
|
* - Add the following to `Link Binary With Libraries`: `libRCTPushNotification.a`
|
2016-01-06 18:19:09 +00:00
|
|
|
|
*
|
|
|
|
|
* Finally, to enable support for `notification` and `register` events you need to augment your AppDelegate.
|
2015-10-14 16:06:37 +00:00
|
|
|
|
*
|
|
|
|
|
* At the top of your `AppDelegate.m`:
|
|
|
|
|
*
|
2017-04-28 12:35:12 +00:00
|
|
|
|
* `#import <React/RCTPushNotificationManager.h>`
|
2015-10-14 16:06:37 +00:00
|
|
|
|
*
|
|
|
|
|
* And then in your AppDelegate implementation add the following:
|
|
|
|
|
*
|
|
|
|
|
* ```
|
2016-01-05 10:42:18 +00:00
|
|
|
|
* // Required to register for notifications
|
|
|
|
|
* - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
|
|
|
|
|
* {
|
|
|
|
|
* [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
|
|
|
|
|
* }
|
|
|
|
|
* // Required for the register event.
|
2015-10-14 16:06:37 +00:00
|
|
|
|
* - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
|
|
|
|
|
* {
|
2016-01-05 10:42:18 +00:00
|
|
|
|
* [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
|
2015-10-14 16:06:37 +00:00
|
|
|
|
* }
|
2016-10-27 04:35:33 +00:00
|
|
|
|
* // Required for the notification event. You must call the completion handler after handling the remote notification.
|
|
|
|
|
* - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
|
2016-12-02 23:18:12 +00:00
|
|
|
|
* fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
|
2016-10-27 04:35:33 +00:00
|
|
|
|
* {
|
2016-12-02 23:18:12 +00:00
|
|
|
|
* [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
|
2016-10-27 04:35:33 +00:00
|
|
|
|
* }
|
2016-09-06 18:01:31 +00:00
|
|
|
|
* // Required for the registrationError event.
|
|
|
|
|
* - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
|
|
|
|
|
* {
|
|
|
|
|
* [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
|
|
|
|
|
* }
|
2016-02-09 13:45:23 +00:00
|
|
|
|
* // Required for the localNotification event.
|
|
|
|
|
* - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
|
|
|
|
|
* {
|
|
|
|
|
* [RCTPushNotificationManager didReceiveLocalNotification:notification];
|
|
|
|
|
* }
|
2015-10-14 16:06:37 +00:00
|
|
|
|
* ```
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
2015-03-12 19:51:44 +00:00
|
|
|
|
class PushNotificationIOS {
|
2015-03-24 16:26:16 +00:00
|
|
|
|
_data: Object;
|
|
|
|
|
_alert: string | Object;
|
|
|
|
|
_sound: string;
|
2017-06-06 00:48:49 +00:00
|
|
|
|
_category: string;
|
2017-06-19 23:37:31 +00:00
|
|
|
|
_contentAvailable: ContentAvailable;
|
2015-03-24 16:26:16 +00:00
|
|
|
|
_badgeCount: number;
|
2016-10-27 04:35:33 +00:00
|
|
|
|
_notificationId: string;
|
|
|
|
|
_isRemote: boolean;
|
2017-07-11 17:55:48 +00:00
|
|
|
|
_remoteNotificationCompleteCallbackCalled: boolean;
|
2016-10-27 04:35:33 +00:00
|
|
|
|
|
|
|
|
|
static FetchResult: FetchResult = {
|
|
|
|
|
NewData: 'UIBackgroundFetchResultNewData',
|
|
|
|
|
NoData: 'UIBackgroundFetchResultNoData',
|
|
|
|
|
ResultFailed: 'UIBackgroundFetchResultFailed',
|
|
|
|
|
};
|
2015-03-24 16:26:16 +00:00
|
|
|
|
|
2015-07-14 16:07:24 +00:00
|
|
|
|
/**
|
|
|
|
|
* Schedules the localNotification for immediate presentation.
|
|
|
|
|
*
|
|
|
|
|
* details is an object containing:
|
|
|
|
|
*
|
|
|
|
|
* - `alertBody` : The message displayed in the notification alert.
|
2016-02-19 13:18:24 +00:00
|
|
|
|
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
|
2016-01-07 13:01:11 +00:00
|
|
|
|
* - `soundName` : The sound played when the notification is fired (optional).
|
2017-05-28 22:26:01 +00:00
|
|
|
|
* - `isSilent` : If true, the notification will appear without sound (optional).
|
2016-02-19 13:18:24 +00:00
|
|
|
|
* - `category` : The category of this notification, required for actionable notifications (optional).
|
|
|
|
|
* - `userInfo` : An optional object containing additional notification data.
|
2016-07-13 05:56:00 +00:00
|
|
|
|
* - `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.
|
2015-07-14 16:07:24 +00:00
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
* - `alertBody` : The message displayed in the notification alert.
|
2016-02-19 13:18:24 +00:00
|
|
|
|
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
|
2016-01-07 13:01:11 +00:00
|
|
|
|
* - `soundName` : The sound played when the notification is fired (optional).
|
2017-05-28 22:26:01 +00:00
|
|
|
|
* - `isSilent` : If true, the notification will appear without sound (optional).
|
2016-02-19 13:18:24 +00:00
|
|
|
|
* - `category` : The category of this notification, required for actionable notifications (optional).
|
2016-02-09 13:45:23 +00:00
|
|
|
|
* - `userInfo` : An optional object containing additional notification data.
|
2016-07-13 05:56:00 +00:00
|
|
|
|
* - `applicationIconBadgeNumber` (optional) : The number to display as the app's icon badge. Setting the number to 0 removes the icon badge.
|
2016-10-11 14:39:48 +00:00
|
|
|
|
* - `repeatInterval` : The interval to repeat as a string. Possible values: `minute`, `hour`, `day`, `week`, `month`, `year`.
|
2015-07-14 16:07:24 +00:00
|
|
|
|
*/
|
|
|
|
|
static scheduleLocalNotification(details: Object) {
|
|
|
|
|
RCTPushNotificationManager.scheduleLocalNotification(details);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 17:39:38 +00:00
|
|
|
|
/**
|
|
|
|
|
* Cancels all scheduled localNotifications
|
|
|
|
|
*/
|
|
|
|
|
static cancelAllLocalNotifications() {
|
|
|
|
|
RCTPushNotificationManager.cancelAllLocalNotifications();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 18:48:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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: [Object]) => void): void {
|
|
|
|
|
RCTPushNotificationManager.getDeliveredNotifications(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the specified notifications from Notification Center
|
|
|
|
|
*
|
|
|
|
|
* @param identifiers Array of notification identifiers
|
|
|
|
|
*/
|
|
|
|
|
static removeDeliveredNotifications(identifiers: [string]): void {
|
|
|
|
|
RCTPushNotificationManager.removeDeliveredNotifications(identifiers);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets the badge number for the app icon on the home screen
|
|
|
|
|
*/
|
|
|
|
|
static setApplicationIconBadgeNumber(number: number) {
|
2015-03-24 16:26:16 +00:00
|
|
|
|
RCTPushNotificationManager.setApplicationIconBadgeNumber(number);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the current badge number for the app icon on the home screen
|
|
|
|
|
*/
|
|
|
|
|
static getApplicationIconBadgeNumber(callback: Function) {
|
2015-03-24 16:26:16 +00:00
|
|
|
|
RCTPushNotificationManager.getApplicationIconBadgeNumber(callback);
|
|
|
|
|
}
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
2016-02-09 13:45:23 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 16:18:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the local notifications that are currently scheduled.
|
|
|
|
|
*/
|
|
|
|
|
static getScheduledLocalNotifications(callback: Function) {
|
|
|
|
|
RCTPushNotificationManager.getScheduledLocalNotifications(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2016-04-01 16:55:21 +00:00
|
|
|
|
* Attaches a listener to remote or local notification events while the app is running
|
2015-06-03 21:11:20 +00:00
|
|
|
|
* in the foreground or the background.
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*
|
2015-06-03 21:11:20 +00:00
|
|
|
|
* Valid events are:
|
|
|
|
|
*
|
|
|
|
|
* - `notification` : Fired when a remote notification is received. The
|
|
|
|
|
* handler will be invoked with an instance of `PushNotificationIOS`.
|
2016-04-01 16:55:21 +00:00
|
|
|
|
* - `localNotification` : Fired when a local notification is received. The
|
|
|
|
|
* handler will be invoked with an instance of `PushNotificationIOS`.
|
2015-06-03 21:11:20 +00:00
|
|
|
|
* - `register`: Fired when the user registers for remote notifications. The
|
|
|
|
|
* handler will be invoked with a hex string representing the deviceToken.
|
2016-09-06 18:01:31 +00:00
|
|
|
|
* - `registrationError`: 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}.
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
2016-09-06 18:01:31 +00:00
|
|
|
|
static addEventListener(type: PushNotificationEventName, handler: Function) {
|
2015-03-26 16:28:03 +00:00
|
|
|
|
invariant(
|
2016-09-06 18:01:31 +00:00
|
|
|
|
type === 'notification' || type === 'register' || type === 'registrationError' || type === 'localNotification',
|
|
|
|
|
'PushNotificationIOS only supports `notification`, `register`, `registrationError`, and `localNotification` events'
|
2015-03-12 19:51:44 +00:00
|
|
|
|
);
|
2015-06-11 20:34:38 +00:00
|
|
|
|
var listener;
|
2015-06-03 21:11:20 +00:00
|
|
|
|
if (type === 'notification') {
|
2016-05-27 17:14:12 +00:00
|
|
|
|
listener = PushNotificationEmitter.addListener(
|
2015-06-03 21:11:20 +00:00
|
|
|
|
DEVICE_NOTIF_EVENT,
|
|
|
|
|
(notifData) => {
|
|
|
|
|
handler(new PushNotificationIOS(notifData));
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-02-09 13:45:23 +00:00
|
|
|
|
} else if (type === 'localNotification') {
|
2016-05-27 17:14:12 +00:00
|
|
|
|
listener = PushNotificationEmitter.addListener(
|
2016-02-09 13:45:23 +00:00
|
|
|
|
DEVICE_LOCAL_NOTIF_EVENT,
|
|
|
|
|
(notifData) => {
|
|
|
|
|
handler(new PushNotificationIOS(notifData));
|
|
|
|
|
}
|
|
|
|
|
);
|
2015-06-03 21:11:20 +00:00
|
|
|
|
} else if (type === 'register') {
|
2016-05-27 17:14:12 +00:00
|
|
|
|
listener = PushNotificationEmitter.addListener(
|
2015-06-03 21:11:20 +00:00
|
|
|
|
NOTIF_REGISTER_EVENT,
|
|
|
|
|
(registrationInfo) => {
|
|
|
|
|
handler(registrationInfo.deviceToken);
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-09-06 18:01:31 +00:00
|
|
|
|
} else if (type === 'registrationError') {
|
|
|
|
|
listener = PushNotificationEmitter.addListener(
|
|
|
|
|
NOTIF_REGISTRATION_ERROR_EVENT,
|
|
|
|
|
(errorInfo) => {
|
|
|
|
|
handler(errorInfo);
|
|
|
|
|
}
|
|
|
|
|
);
|
2015-06-03 21:11:20 +00:00
|
|
|
|
}
|
2016-11-14 20:44:16 +00:00
|
|
|
|
_notifHandlers.set(type, listener);
|
2015-03-12 19:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* Removes the event listener. Do this in `componentWillUnmount` to prevent
|
|
|
|
|
* memory leaks
|
|
|
|
|
*/
|
2016-09-06 18:01:31 +00:00
|
|
|
|
static removeEventListener(type: PushNotificationEventName, handler: Function) {
|
2016-05-27 17:14:12 +00:00
|
|
|
|
invariant(
|
2016-09-06 18:01:31 +00:00
|
|
|
|
type === 'notification' || type === 'register' || type === 'registrationError' || type === 'localNotification',
|
|
|
|
|
'PushNotificationIOS only supports `notification`, `register`, `registrationError`, and `localNotification` events'
|
2016-05-27 17:14:12 +00:00
|
|
|
|
);
|
2016-11-14 20:44:16 +00:00
|
|
|
|
var listener = _notifHandlers.get(type);
|
2016-05-27 17:14:12 +00:00
|
|
|
|
if (!listener) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
listener.remove();
|
2016-11-14 20:44:16 +00:00
|
|
|
|
_notifHandlers.delete(type);
|
2016-05-27 17:14:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2015-06-03 21:11:20 +00:00
|
|
|
|
* Requests notification permissions from iOS, prompting the user's
|
|
|
|
|
* dialog box. By default, it will request all notification permissions, but
|
|
|
|
|
* a subset of these can be requested by passing a map of requested
|
|
|
|
|
* permissions.
|
|
|
|
|
* The following permissions are supported:
|
|
|
|
|
*
|
|
|
|
|
* - `alert`
|
|
|
|
|
* - `badge`
|
|
|
|
|
* - `sound`
|
|
|
|
|
*
|
|
|
|
|
* If a map is provided to the method, only the permissions with truthy values
|
|
|
|
|
* will be requested.
|
2016-06-03 23:14:44 +00:00
|
|
|
|
|
|
|
|
|
* This method returns a promise that will resolve when the user accepts,
|
|
|
|
|
* rejects, or if the permissions were previously rejected. The promise
|
|
|
|
|
* resolves to the current state of the permission.
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
2015-06-03 21:11:20 +00:00
|
|
|
|
static requestPermissions(permissions?: {
|
|
|
|
|
alert?: boolean,
|
|
|
|
|
badge?: boolean,
|
|
|
|
|
sound?: boolean
|
2016-06-03 23:14:44 +00:00
|
|
|
|
}): Promise<{
|
|
|
|
|
alert: boolean,
|
|
|
|
|
badge: boolean,
|
|
|
|
|
sound: boolean
|
|
|
|
|
}> {
|
2015-06-03 21:11:20 +00:00
|
|
|
|
var requestedPermissions = {};
|
|
|
|
|
if (permissions) {
|
|
|
|
|
requestedPermissions = {
|
|
|
|
|
alert: !!permissions.alert,
|
|
|
|
|
badge: !!permissions.badge,
|
|
|
|
|
sound: !!permissions.sound
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
requestedPermissions = {
|
|
|
|
|
alert: true,
|
|
|
|
|
badge: true,
|
|
|
|
|
sound: true
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-06-03 23:14:44 +00:00
|
|
|
|
return RCTPushNotificationManager.requestPermissions(requestedPermissions);
|
2015-03-24 16:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-15 19:14:23 +00:00
|
|
|
|
/**
|
|
|
|
|
* Unregister for all remote notifications received via Apple Push Notification service.
|
|
|
|
|
*
|
|
|
|
|
* You should call this method in rare circumstances only, such as when a new version of
|
|
|
|
|
* the app removes support for all types of remote notifications. Users can temporarily
|
|
|
|
|
* prevent apps from receiving remote notifications through the Notifications section of
|
|
|
|
|
* the Settings app. Apps unregistered through this method can always re-register.
|
|
|
|
|
*/
|
|
|
|
|
static abandonPermissions() {
|
|
|
|
|
RCTPushNotificationManager.abandonPermissions();
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* See what push permissions are currently enabled. `callback` will be
|
|
|
|
|
* invoked with a `permissions` object:
|
|
|
|
|
*
|
|
|
|
|
* - `alert` :boolean
|
|
|
|
|
* - `badge` :boolean
|
|
|
|
|
* - `sound` :boolean
|
|
|
|
|
*/
|
|
|
|
|
static checkPermissions(callback: Function) {
|
2015-03-24 16:26:16 +00:00
|
|
|
|
invariant(
|
|
|
|
|
typeof callback === 'function',
|
|
|
|
|
'Must provide a valid callback'
|
|
|
|
|
);
|
|
|
|
|
RCTPushNotificationManager.checkPermissions(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
|
/**
|
2016-08-03 19:17:05 +00:00
|
|
|
|
* This method returns a promise that resolves to either the notification
|
|
|
|
|
* object if the app was launched by a push notification, or `null` otherwise.
|
2016-05-27 17:14:12 +00:00
|
|
|
|
*/
|
|
|
|
|
static getInitialNotification(): Promise<?PushNotificationIOS> {
|
|
|
|
|
return RCTPushNotificationManager.getInitialNotification().then(notification => {
|
|
|
|
|
return notification && new PushNotificationIOS(notification);
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-03-17 02:01:28 +00:00
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2015-12-15 17:08:39 +00:00
|
|
|
|
* You will never need to instantiate `PushNotificationIOS` yourself.
|
2015-03-26 16:28:03 +00:00
|
|
|
|
* Listening to the `notification` event and invoking
|
2016-07-14 07:12:25 +00:00
|
|
|
|
* `getInitialNotification` is sufficient
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
2015-09-18 17:34:37 +00:00
|
|
|
|
constructor(nativeNotif: Object) {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
this._data = {};
|
2017-07-11 17:55:48 +00:00
|
|
|
|
this._remoteNotificationCompleteCallbackCalled = false;
|
2016-10-27 04:35:33 +00:00
|
|
|
|
this._isRemote = nativeNotif.remote;
|
|
|
|
|
if (this._isRemote) {
|
|
|
|
|
this._notificationId = nativeNotif.notificationId;
|
|
|
|
|
}
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
2016-06-14 13:02:36 +00:00
|
|
|
|
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;
|
2017-06-06 00:48:49 +00:00
|
|
|
|
this._category = notifVal.category;
|
2017-06-19 23:37:31 +00:00
|
|
|
|
this._contentAvailable = notifVal['content-available'];
|
2016-06-14 13:02:36 +00:00
|
|
|
|
} 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;
|
2017-06-06 00:48:49 +00:00
|
|
|
|
this._category = nativeNotif.category;
|
2016-06-14 13:02:36 +00:00
|
|
|
|
}
|
2015-03-12 19:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 04:35:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* This method is available for remote notifications that have been received via:
|
|
|
|
|
* `application:didReceiveRemoteNotification:fetchCompletionHandler:`
|
|
|
|
|
* https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:
|
|
|
|
|
*
|
|
|
|
|
* Call this to execute when the remote notification handling is complete. When
|
|
|
|
|
* calling this block, pass in the fetch result value that best describes
|
|
|
|
|
* the results of your operation. You *must* call this handler and should do so
|
|
|
|
|
* as soon as possible. For a list of possible values, see `PushNotificationIOS.FetchResult`.
|
|
|
|
|
*
|
|
|
|
|
* If you do not call this method your background remote notifications could
|
|
|
|
|
* be throttled, to read more about it see the above documentation link.
|
|
|
|
|
*/
|
|
|
|
|
finish(fetchResult: FetchResult) {
|
2017-07-11 17:55:48 +00:00
|
|
|
|
if (!this._isRemote || !this._notificationId || this._remoteNotificationCompleteCallbackCalled) {
|
2016-10-27 04:35:33 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-11 17:55:48 +00:00
|
|
|
|
this._remoteNotificationCompleteCallbackCalled = true;
|
2016-10-27 04:35:33 +00:00
|
|
|
|
|
|
|
|
|
RCTPushNotificationManager.onFinishRemoteNotification(this._notificationId, fetchResult);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* An alias for `getAlert` to get the notification's main message string
|
|
|
|
|
*/
|
2015-03-24 16:26:16 +00:00
|
|
|
|
getMessage(): ?string | ?Object {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
// alias because "alert" is an ambiguous name
|
|
|
|
|
return this._alert;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the sound string from the `aps` object
|
|
|
|
|
*/
|
2015-03-24 16:26:16 +00:00
|
|
|
|
getSound(): ?string {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
return this._sound;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 00:48:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the category string from the `aps` object
|
|
|
|
|
*/
|
|
|
|
|
getCategory(): ?string {
|
|
|
|
|
return this._category;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the notification's main message from the `aps` object
|
|
|
|
|
*/
|
2015-03-24 16:26:16 +00:00
|
|
|
|
getAlert(): ?string | ?Object {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
return this._alert;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 23:37:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the content-available number from the `aps` object
|
|
|
|
|
*/
|
|
|
|
|
getContentAvailable(): ContentAvailable {
|
|
|
|
|
return this._contentAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the badge count number from the `aps` object
|
|
|
|
|
*/
|
2015-03-24 16:26:16 +00:00
|
|
|
|
getBadgeCount(): ?number {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
return this._badgeCount;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the data object on the notif
|
|
|
|
|
*/
|
2015-03-24 16:26:16 +00:00
|
|
|
|
getData(): ?Object {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
return this._data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = PushNotificationIOS;
|