2015-03-12 00:16:50 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +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 00:16:50 +00:00
|
|
|
*
|
|
|
|
* @providesModule PushNotificationIOS
|
2015-03-24 00:09:14 +00:00
|
|
|
* @flow
|
2015-03-12 00:16:50 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-16 19:33:02 +00:00
|
|
|
var NativeModules = require('NativeModules');
|
2015-03-12 00:16:50 +00:00
|
|
|
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
2015-03-24 12:31:11 +00:00
|
|
|
var RCTPushNotificationManager = require('NativeModules').PushNotificationManager;
|
|
|
|
var invariant = require('invariant');
|
2015-03-16 19:33:02 +00:00
|
|
|
|
2015-03-12 00:16:50 +00:00
|
|
|
var _notifHandlers = {};
|
2015-03-24 12:31:11 +00:00
|
|
|
var _initialNotification = RCTPushNotificationManager &&
|
|
|
|
RCTPushNotificationManager.initialNotification;
|
2015-03-12 00:16:50 +00:00
|
|
|
|
|
|
|
var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
|
|
|
|
|
|
|
|
class PushNotificationIOS {
|
2015-03-24 00:09:14 +00:00
|
|
|
_data: Object;
|
|
|
|
_alert: string | Object;
|
|
|
|
_sound: string;
|
|
|
|
_badgeCount: number;
|
2015-03-12 00:16:50 +00:00
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
static setApplicationIconBadgeNumber(number) {
|
|
|
|
RCTPushNotificationManager.setApplicationIconBadgeNumber(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getApplicationIconBadgeNumber(callback) {
|
|
|
|
RCTPushNotificationManager.getApplicationIconBadgeNumber(callback);
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:16:50 +00:00
|
|
|
static addEventListener(type, handler) {
|
|
|
|
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
|
|
|
|
DEVICE_NOTIF_EVENT,
|
|
|
|
(notifData) => {
|
|
|
|
handler(new PushNotificationIOS(notifData));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:31:11 +00:00
|
|
|
static requestPermissions() {
|
|
|
|
RCTPushNotificationManager.requestPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
static checkPermissions(callback) {
|
|
|
|
invariant(
|
|
|
|
typeof callback === 'function',
|
|
|
|
'Must provide a valid callback'
|
|
|
|
);
|
|
|
|
RCTPushNotificationManager.checkPermissions(callback);
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:16:50 +00:00
|
|
|
static removeEventListener(type, handler) {
|
|
|
|
if (!_notifHandlers[handler]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_notifHandlers[handler].remove();
|
|
|
|
_notifHandlers[handler] = null;
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:33:02 +00:00
|
|
|
|
|
|
|
static popInitialNotification() {
|
|
|
|
var initialNotification = _initialNotification &&
|
|
|
|
new PushNotificationIOS(_initialNotification);
|
|
|
|
_initialNotification = null;
|
|
|
|
return initialNotification;
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:16:50 +00:00
|
|
|
constructor(nativeNotif) {
|
|
|
|
this._data = {};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:09:14 +00:00
|
|
|
getMessage(): ?string | ?Object {
|
2015-03-12 00:16:50 +00:00
|
|
|
// alias because "alert" is an ambiguous name
|
|
|
|
return this._alert;
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:09:14 +00:00
|
|
|
getSound(): ?string {
|
2015-03-12 00:16:50 +00:00
|
|
|
return this._sound;
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:09:14 +00:00
|
|
|
getAlert(): ?string | ?Object {
|
2015-03-12 00:16:50 +00:00
|
|
|
return this._alert;
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:09:14 +00:00
|
|
|
getBadgeCount(): ?number {
|
2015-03-12 00:16:50 +00:00
|
|
|
return this._badgeCount;
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:09:14 +00:00
|
|
|
getData(): ?Object {
|
2015-03-12 00:16:50 +00:00
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PushNotificationIOS;
|