2015-03-12 19:51:44 +00:00
|
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-12 19:51:44 +00:00
|
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
|
* @format
|
2015-03-24 16:26:16 +00:00
|
|
|
|
* @flow
|
2015-03-12 19:51:44 +00:00
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
|
2015-03-12 19:51:44 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
|
const NativeEventEmitter = require('NativeEventEmitter');
|
2018-05-11 02:06:46 +00:00
|
|
|
|
const RCTPushNotificationManager = require('NativeModules')
|
|
|
|
|
.PushNotificationManager;
|
2016-05-27 17:14:12 +00:00
|
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-03-17 02:01:28 +00:00
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
|
const PushNotificationEmitter = new NativeEventEmitter(
|
|
|
|
|
RCTPushNotificationManager,
|
|
|
|
|
);
|
2016-05-27 17:14:12 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2015-03-26 16:28:03 +00:00
|
|
|
|
* Handle push notifications for your app, including permission handling and
|
|
|
|
|
* icon badge number.
|
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html
|
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;
|
2018-01-11 01:40:57 +00:00
|
|
|
|
_threadID: string;
|
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.
|
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#presentlocalnotification
|
2015-07-14 16:07:24 +00:00
|
|
|
|
*/
|
|
|
|
|
static presentLocalNotification(details: Object) {
|
|
|
|
|
RCTPushNotificationManager.presentLocalNotification(details);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Schedules the localNotification for future presentation.
|
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#schedulelocalnotification
|
2015-07-14 16:07:24 +00:00
|
|
|
|
*/
|
|
|
|
|
static scheduleLocalNotification(details: Object) {
|
|
|
|
|
RCTPushNotificationManager.scheduleLocalNotification(details);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 17:39:38 +00:00
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* Cancels all scheduled localNotifications.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#cancelalllocalnotifications
|
2015-10-16 17:39:38 +00:00
|
|
|
|
*/
|
|
|
|
|
static cancelAllLocalNotifications() {
|
|
|
|
|
RCTPushNotificationManager.cancelAllLocalNotifications();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 18:48:04 +00:00
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* Remove all delivered notifications from Notification Center.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#removealldeliverednotifications
|
2017-03-23 18:48:04 +00:00
|
|
|
|
*/
|
|
|
|
|
static removeAllDeliveredNotifications(): void {
|
|
|
|
|
RCTPushNotificationManager.removeAllDeliveredNotifications();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* Provides you with a list of the app’s notifications that are still displayed in Notification Center.
|
2017-03-23 18:48:04 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications
|
2017-03-23 18:48:04 +00:00
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
static getDeliveredNotifications(
|
|
|
|
|
callback: (notifications: Array<Object>) => void,
|
|
|
|
|
): void {
|
2017-03-23 18:48:04 +00:00
|
|
|
|
RCTPushNotificationManager.getDeliveredNotifications(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the specified notifications from Notification Center
|
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#removedeliverednotifications
|
2017-03-23 18:48:04 +00:00
|
|
|
|
*/
|
2017-08-14 18:56:22 +00:00
|
|
|
|
static removeDeliveredNotifications(identifiers: Array<string>): void {
|
2017-03-23 18:48:04 +00:00
|
|
|
|
RCTPushNotificationManager.removeDeliveredNotifications(identifiers);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* Sets the badge number for the app icon on the home screen.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#setapplicationiconbadgenumber
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
|
|
|
|
static setApplicationIconBadgeNumber(number: number) {
|
2015-03-24 16:26:16 +00:00
|
|
|
|
RCTPushNotificationManager.setApplicationIconBadgeNumber(number);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* Gets the current badge number for the app icon on the home screen.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getapplicationiconbadgenumber
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#cancellocalnotification
|
2016-02-09 13:45:23 +00:00
|
|
|
|
*/
|
|
|
|
|
static cancelLocalNotifications(userInfo: Object) {
|
|
|
|
|
RCTPushNotificationManager.cancelLocalNotifications(userInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 16:18:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the local notifications that are currently scheduled.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getscheduledlocalnotifications
|
2016-06-06 16:18:59 +00:00
|
|
|
|
*/
|
|
|
|
|
static getScheduledLocalNotifications(callback: Function) {
|
|
|
|
|
RCTPushNotificationManager.getScheduledLocalNotifications(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* Attaches a listener to remote or local notification events while the app
|
|
|
|
|
* is running in the foreground or the background.
|
2015-06-03 21:11:20 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#addeventlistener
|
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(
|
2018-05-11 02:06:46 +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
|
|
|
|
);
|
2018-05-10 22:44:52 +00:00
|
|
|
|
let listener;
|
2015-06-03 21:11:20 +00:00
|
|
|
|
if (type === 'notification') {
|
2018-05-11 02:06:46 +00:00
|
|
|
|
listener = PushNotificationEmitter.addListener(
|
2015-06-03 21:11:20 +00:00
|
|
|
|
DEVICE_NOTIF_EVENT,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
notifData => {
|
2015-06-03 21:11:20 +00:00
|
|
|
|
handler(new PushNotificationIOS(notifData));
|
2018-05-11 02:06:46 +00:00
|
|
|
|
},
|
2015-06-03 21:11:20 +00:00
|
|
|
|
);
|
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,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
notifData => {
|
2016-02-09 13:45:23 +00:00
|
|
|
|
handler(new PushNotificationIOS(notifData));
|
2018-05-11 02:06:46 +00:00
|
|
|
|
},
|
2016-02-09 13:45:23 +00:00
|
|
|
|
);
|
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,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
registrationInfo => {
|
2015-06-03 21:11:20 +00:00
|
|
|
|
handler(registrationInfo.deviceToken);
|
2018-05-11 02:06:46 +00:00
|
|
|
|
},
|
2015-06-03 21:11:20 +00:00
|
|
|
|
);
|
2016-09-06 18:01:31 +00:00
|
|
|
|
} else if (type === 'registrationError') {
|
|
|
|
|
listener = PushNotificationEmitter.addListener(
|
|
|
|
|
NOTIF_REGISTRATION_ERROR_EVENT,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
errorInfo => {
|
2016-09-06 18:01:31 +00:00
|
|
|
|
handler(errorInfo);
|
2018-05-11 02:06:46 +00:00
|
|
|
|
},
|
2016-09-06 18:01:31 +00:00
|
|
|
|
);
|
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
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* memory leaks.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#removeeventlistener
|
2016-05-27 17:14:12 +00:00
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
static removeEventListener(
|
|
|
|
|
type: PushNotificationEventName,
|
|
|
|
|
handler: Function,
|
|
|
|
|
) {
|
2016-05-27 17:14:12 +00:00
|
|
|
|
invariant(
|
2018-05-11 02:06:46 +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
|
|
|
|
);
|
2018-05-10 22:44:52 +00:00
|
|
|
|
const 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.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#requestpermissions
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
2015-06-03 21:11:20 +00:00
|
|
|
|
static requestPermissions(permissions?: {
|
|
|
|
|
alert?: boolean,
|
|
|
|
|
badge?: boolean,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
sound?: boolean,
|
2016-06-03 23:14:44 +00:00
|
|
|
|
}): Promise<{
|
|
|
|
|
alert: boolean,
|
|
|
|
|
badge: boolean,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
sound: boolean,
|
2016-06-03 23:14:44 +00:00
|
|
|
|
}> {
|
2018-05-10 22:44:52 +00:00
|
|
|
|
let requestedPermissions = {};
|
2015-06-03 21:11:20 +00:00
|
|
|
|
if (permissions) {
|
|
|
|
|
requestedPermissions = {
|
|
|
|
|
alert: !!permissions.alert,
|
|
|
|
|
badge: !!permissions.badge,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
sound: !!permissions.sound,
|
2015-06-03 21:11:20 +00:00
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
requestedPermissions = {
|
|
|
|
|
alert: true,
|
|
|
|
|
badge: true,
|
2018-05-11 02:06:46 +00:00
|
|
|
|
sound: true,
|
2015-06-03 21:11:20 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
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.
|
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#abandonpermissions
|
2015-06-15 19:14:23 +00:00
|
|
|
|
*/
|
|
|
|
|
static abandonPermissions() {
|
|
|
|
|
RCTPushNotificationManager.abandonPermissions();
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* See what push permissions are currently enabled. `callback` will be
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* invoked with a `permissions` object.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#checkpermissions
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
|
|
|
|
static checkPermissions(callback: Function) {
|
2018-05-11 02:06:46 +00:00
|
|
|
|
invariant(typeof callback === 'function', 'Must provide a valid callback');
|
2015-03-24 16:26:16 +00:00
|
|
|
|
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.
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getinitialnotification
|
2016-05-27 17:14:12 +00:00
|
|
|
|
*/
|
|
|
|
|
static getInitialNotification(): Promise<?PushNotificationIOS> {
|
2018-05-11 02:06:46 +00:00
|
|
|
|
return RCTPushNotificationManager.getInitialNotification().then(
|
|
|
|
|
notification => {
|
|
|
|
|
return notification && new PushNotificationIOS(notification);
|
|
|
|
|
},
|
|
|
|
|
);
|
2016-05-27 17:14:12 +00:00
|
|
|
|
}
|
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
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
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
|
2018-05-11 02:06:46 +00:00
|
|
|
|
Object.keys(nativeNotif).forEach(notifKey => {
|
2018-05-10 22:44:52 +00:00
|
|
|
|
const notifVal = nativeNotif[notifKey];
|
2016-06-14 13:02:36 +00:00
|
|
|
|
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'];
|
2018-01-11 01:40:57 +00:00
|
|
|
|
this._threadID = notifVal['thread-id'];
|
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:`
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#finish
|
2016-10-27 04:35:33 +00:00
|
|
|
|
*/
|
2018-01-11 01:40:57 +00:00
|
|
|
|
finish(fetchResult: string) {
|
2018-05-11 02:06:46 +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
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
|
RCTPushNotificationManager.onFinishRemoteNotification(
|
|
|
|
|
this._notificationId,
|
|
|
|
|
fetchResult,
|
|
|
|
|
);
|
2016-10-27 04:35:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getsound
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
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
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getcategory
|
2017-06-06 00:48:49 +00:00
|
|
|
|
*/
|
|
|
|
|
getCategory(): ?string {
|
|
|
|
|
return this._category;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the notification's main message from the `aps` object
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getalert
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
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
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getcontentavailable
|
2017-06-19 23:37:31 +00:00
|
|
|
|
*/
|
|
|
|
|
getContentAvailable(): ContentAvailable {
|
|
|
|
|
return this._contentAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 16:28:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the badge count number from the `aps` object
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getbadgecount
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
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
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getdata
|
2015-03-26 16:28:03 +00:00
|
|
|
|
*/
|
2015-03-24 16:26:16 +00:00
|
|
|
|
getData(): ?Object {
|
2015-03-12 19:51:44 +00:00
|
|
|
|
return this._data;
|
|
|
|
|
}
|
2018-01-11 01:40:57 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the thread ID on the notif
|
2018-02-22 15:04:35 +00:00
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
|
* See https://facebook.github.io/react-native/docs/pushnotificationios.html#getthreadid
|
2018-01-11 01:40:57 +00:00
|
|
|
|
*/
|
|
|
|
|
getThreadID(): ?string {
|
|
|
|
|
return this._threadID;
|
|
|
|
|
}
|
2015-03-12 19:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = PushNotificationIOS;
|