2015-03-12 00:16:50 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* @providesModule PushNotificationIOS
|
|
|
|
*/
|
|
|
|
'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-16 19:33:02 +00:00
|
|
|
var RCTPushNotificationManager = NativeModules.RCTPushNotificationManager;
|
2015-03-17 01:13:06 +00:00
|
|
|
if (RCTPushNotificationManager) {
|
|
|
|
var _initialNotification = RCTPushNotificationManager.initialNotification;
|
|
|
|
}
|
2015-03-16 19:33:02 +00:00
|
|
|
|
2015-03-12 00:16:50 +00:00
|
|
|
var _notifHandlers = {};
|
|
|
|
|
|
|
|
var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
|
|
|
|
|
|
|
|
class PushNotificationIOS {
|
|
|
|
|
|
|
|
static addEventListener(type, handler) {
|
|
|
|
_notifHandlers[handler] = RCTDeviceEventEmitter.addListener(
|
|
|
|
DEVICE_NOTIF_EVENT,
|
|
|
|
(notifData) => {
|
|
|
|
handler(new PushNotificationIOS(notifData));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getMessage() {
|
|
|
|
// alias because "alert" is an ambiguous name
|
|
|
|
return this._alert;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSound() {
|
|
|
|
return this._sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAlert() {
|
|
|
|
return this._alert;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBadgeCount() {
|
|
|
|
return this._badgeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
getData() {
|
|
|
|
return this._data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PushNotificationIOS;
|