mirror of
https://github.com/status-im/react-native.git
synced 2025-01-15 20:15:11 +00:00
[ReactNative] PushNotificationIOS listener Map
This commit is contained in:
parent
f0bba0c111
commit
1b9067a3e3
@ -11,11 +11,12 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var Map = require('Map');
|
||||||
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||||
var RCTPushNotificationManager = require('NativeModules').PushNotificationManager;
|
var RCTPushNotificationManager = require('NativeModules').PushNotificationManager;
|
||||||
var invariant = require('invariant');
|
var invariant = require('invariant');
|
||||||
|
|
||||||
var _notifHandlers = {};
|
var _notifHandlers = new Map();
|
||||||
var _initialNotification = RCTPushNotificationManager &&
|
var _initialNotification = RCTPushNotificationManager &&
|
||||||
RCTPushNotificationManager.initialNotification;
|
RCTPushNotificationManager.initialNotification;
|
||||||
|
|
||||||
@ -65,21 +66,23 @@ class PushNotificationIOS {
|
|||||||
type === 'notification' || type === 'register',
|
type === 'notification' || type === 'register',
|
||||||
'PushNotificationIOS only supports `notification` and `register` events'
|
'PushNotificationIOS only supports `notification` and `register` events'
|
||||||
);
|
);
|
||||||
|
var listener;
|
||||||
if (type === 'notification') {
|
if (type === 'notification') {
|
||||||
_notifHandlers[String(handler)] = RCTDeviceEventEmitter.addListener(
|
listener = RCTDeviceEventEmitter.addListener(
|
||||||
DEVICE_NOTIF_EVENT,
|
DEVICE_NOTIF_EVENT,
|
||||||
(notifData) => {
|
(notifData) => {
|
||||||
handler(new PushNotificationIOS(notifData));
|
handler(new PushNotificationIOS(notifData));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} else if (type === 'register') {
|
} else if (type === 'register') {
|
||||||
_notifHandlers[String(handler)] = RCTDeviceEventEmitter.addListener(
|
listener = RCTDeviceEventEmitter.addListener(
|
||||||
NOTIF_REGISTER_EVENT,
|
NOTIF_REGISTER_EVENT,
|
||||||
(registrationInfo) => {
|
(registrationInfo) => {
|
||||||
handler(registrationInfo.deviceToken);
|
handler(registrationInfo.deviceToken);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
_notifHandlers.set(handler, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,11 +146,12 @@ class PushNotificationIOS {
|
|||||||
type === 'notification' || type === 'register',
|
type === 'notification' || type === 'register',
|
||||||
'PushNotificationIOS only supports `notification` and `register` events'
|
'PushNotificationIOS only supports `notification` and `register` events'
|
||||||
);
|
);
|
||||||
if (!_notifHandlers[String(handler)]) {
|
var listener = _notifHandlers.get(handler);
|
||||||
|
if (!listener) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_notifHandlers[String(handler)].remove();
|
listener.remove();
|
||||||
_notifHandlers[String(handler)] = null;
|
_notifHandlers.delete(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user