Skip to content

Commit cc79a6a

Browse files
committed
refactor: simplify / remove / refine codes, support didLoadWithEvents
* remove unused codes, keep this lib simple, lots functionalities should be easily implemented in the modern RN eco system - remove requestPermission ( ican use PushNotificationIOS ) - remove getting background state ( can use AppState ) * use NativeEventEmitter instead the deprecated DeviceEventEmitter * support caching events before js initialize you can subscribe `didLoadWithEvents` when app ready
1 parent dfee525 commit cc79a6a

File tree

3 files changed

+152
-321
lines changed

3 files changed

+152
-321
lines changed

index.js

Lines changed: 35 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -2,127 +2,75 @@
22

33
import {
44
NativeModules,
5-
DeviceEventEmitter,
5+
NativeEventEmitter,
66
Platform,
77
} from 'react-native';
88

9-
var RNVoipPushNotificationManager = NativeModules.RNVoipPushNotificationManager;
10-
var invariant = require('fbjs/lib/invariant');
9+
const RNVoipPushNotificationManager = NativeModules.RNVoipPushNotificationManager;
1110

12-
var _notifHandlers = new Map();
11+
const eventEmitter = new NativeEventEmitter(RNVoipPushNotificationManager);
12+
const _eventHandlers = new Map();
1313

14-
var DEVICE_NOTIF_EVENT = 'voipRemoteNotificationReceived';
15-
var NOTIF_REGISTER_EVENT = 'voipRemoteNotificationsRegistered';
16-
var DEVICE_LOCAL_NOTIF_EVENT = 'voipLocalNotificationReceived';
14+
// --- native unique event names
15+
const RNVoipPushRemoteNotificationsRegisteredEvent = "RNVoipPushRemoteNotificationsRegisteredEvent"; // --- 'register'
16+
const RNVoipPushRemoteNotificationReceivedEvent = "RNVoipPushRemoteNotificationReceivedEvent"; // --- 'notification'
17+
const RNVoipPushDidLoadWithEvents = "RNVoipPushDidLoadWithEvents"; // --- 'didLoadWithEvents'
1718

1819
export default class RNVoipPushNotification {
1920

20-
static wakeupByPush = (Platform.OS == 'ios' && RNVoipPushNotificationManager.wakeupByPush === 'true');
21-
22-
/**
23-
* Schedules the localNotification for immediate presentation.
24-
*
25-
* details is an object containing:
26-
*
27-
* - `alertBody` : The message displayed in the notification alert.
28-
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
29-
* - `soundName` : The sound played when the notification is fired (optional).
30-
* - `category` : The category of this notification, required for actionable notifications (optional).
31-
* - `userInfo` : An optional object containing additional notification data.
32-
*/
33-
static presentLocalNotification(details) {
34-
RNVoipPushNotificationManager.presentLocalNotification(details);
35-
}
36-
3721
/**
3822
* Attaches a listener to remote notification events while the app is running
3923
* in the foreground or the background.
4024
*
4125
* Valid events are:
4226
*
43-
* - `notification` : Fired when a remote notification is received. The
44-
* handler will be invoked with an instance of `PushNotificationIOS`.
45-
* - `register`: Fired when the user registers for remote notifications. The
46-
* handler will be invoked with a hex string representing the deviceToken.
27+
* - `notification` : Fired when a remote notification is received.
28+
* - `register`: Fired when the user registers for remote notifications.
29+
* - `didLoadWithEvents`: Fired when the user have initially subscribed any listener and has cached events already.
4730
*/
4831
static addEventListener(type, handler) {
49-
invariant(
50-
type === 'notification' || type === 'register' || type === 'localNotification',
51-
'RNVoipPushNotificationManager only supports `notification`, `register` and `localNotification` events'
52-
);
53-
var listener;
32+
let listener;
5433
if (type === 'notification') {
55-
listener = DeviceEventEmitter.addListener(
56-
DEVICE_NOTIF_EVENT,
57-
(notifData) => {
58-
handler(new RNVoipPushNotification(notifData));
34+
listener = eventEmitter.addListener(
35+
RNVoipPushRemoteNotificationReceivedEvent,
36+
(notificationPayload) => {
37+
handler(notificationPayload);
5938
}
6039
);
61-
} else if (type === 'localNotification') {
62-
listener = DeviceEventEmitter.addListener(
63-
DEVICE_LOCAL_NOTIF_EVENT,
64-
(notifData) => {
65-
handler(new RNVoipPushNotification(notifData));
40+
} else if (type === 'register') {
41+
listener = eventEmitter.addListener(
42+
RNVoipPushRemoteNotificationsRegisteredEvent,
43+
(deviceToken) => {
44+
handler(deviceToken);
6645
}
6746
);
68-
} else if (type === 'register') {
69-
listener = DeviceEventEmitter.addListener(
70-
NOTIF_REGISTER_EVENT,
71-
(registrationInfo) => {
72-
handler(registrationInfo.deviceToken);
47+
} else if (type === 'didLoadWithEvents') {
48+
listener = eventEmitter.addListener(
49+
RNVoipPushDidLoadWithEvents,
50+
(events) => {
51+
handler(events);
7352
}
7453
);
54+
} else {
55+
return;
7556
}
76-
_notifHandlers.set(handler, listener);
57+
58+
// --- we only support one listener at a time, remove to prevent leak
59+
RNVoipPushNotification.removeEventListener(type);
60+
_eventHandlers.set(type, listener);
7761
}
7862

7963
/**
8064
* Removes the event listener. Do this in `componentWillUnmount` to prevent
8165
* memory leaks
8266
*/
83-
static removeEventListener(type, handler) {
84-
invariant(
85-
type === 'notification' || type === 'register' || type === 'localNotification',
86-
'RNVoipPushNotification only supports `notification`, `register` and `localNotification` events'
87-
);
88-
var listener = _notifHandlers.get(handler);
67+
static removeEventListener(type) {
68+
let listener = _eventHandlers.get(type);
8969
if (!listener) {
9070
return;
9171
}
9272
listener.remove();
93-
_notifHandlers.delete(handler);
94-
}
95-
96-
/**
97-
* Requests notification permissions from iOS, prompting the user's
98-
* dialog box. By default, it will request all notification permissions, but
99-
* a subset of these can be requested by passing a map of requested
100-
* permissions.
101-
* The following permissions are supported:
102-
*
103-
* - `alert`
104-
* - `badge`
105-
* - `sound`
106-
*
107-
* If a map is provided to the method, only the permissions with truthy values
108-
* will be requested.
109-
*/
110-
static requestPermissions(permissions) {
111-
var requestedPermissions = {};
112-
if (permissions) {
113-
requestedPermissions = {
114-
alert: !!permissions.alert,
115-
badge: !!permissions.badge,
116-
sound: !!permissions.sound
117-
};
118-
} else {
119-
requestedPermissions = {
120-
alert: true,
121-
badge: true,
122-
sound: true
123-
};
124-
}
125-
RNVoipPushNotificationManager.requestPermissions(requestedPermissions);
73+
_eventHandlers.delete(type);
12674
}
12775

12876
/**
@@ -150,63 +98,4 @@ export default class RNVoipPushNotification {
15098
RNVoipPushNotificationManager.onVoipNotificationCompleted(uuid);
15199
}
152100

153-
/**
154-
* You will never need to instantiate `RNVoipPushNotification` yourself.
155-
* Listening to the `notification` event and invoking
156-
* `popInitialNotification` is sufficient
157-
*/
158-
constructor(nativeNotif) {
159-
this._data = {};
160-
161-
// Extract data from Apple's `aps` dict as defined:
162-
163-
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
164-
165-
Object.keys(nativeNotif).forEach((notifKey) => {
166-
var notifVal = nativeNotif[notifKey];
167-
if (notifKey === 'aps') {
168-
this._alert = notifVal.alert;
169-
this._sound = notifVal.sound;
170-
this._badgeCount = notifVal.badge;
171-
} else {
172-
this._data[notifKey] = notifVal;
173-
}
174-
});
175-
}
176-
177-
/**
178-
* An alias for `getAlert` to get the notification's main message string
179-
*/
180-
getMessage() {
181-
// alias because "alert" is an ambiguous name
182-
return this._alert;
183-
}
184-
185-
/**
186-
* Gets the sound string from the `aps` object
187-
*/
188-
getSound() {
189-
return this._sound;
190-
}
191-
192-
/**
193-
* Gets the notification's main message from the `aps` object
194-
*/
195-
getAlert() {
196-
return this._alert;
197-
}
198-
199-
/**
200-
* Gets the badge count number from the `aps` object
201-
*/
202-
getBadgeCount() {
203-
return this._badgeCount;
204-
}
205-
206-
/**
207-
* Gets the data object on the notif
208-
*/
209-
getData() {
210-
return this._data;
211-
}
212101
}

ios/RNVoipPushNotification/RNVoipPushNotificationManager.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@
77
//
88

99
#import <Foundation/Foundation.h>
10-
1110
#import <React/RCTBridgeModule.h>
11+
#import <React/RCTEventEmitter.h>
1212

13-
@interface RNVoipPushNotificationManager : NSObject <RCTBridgeModule>
13+
@interface RNVoipPushNotificationManager : RCTEventEmitter <RCTBridgeModule>
1414

1515
typedef void (^RNVoipPushNotificationCompletion)(void);
1616

1717
@property (nonatomic, strong) NSMutableDictionary<NSString *, RNVoipPushNotificationCompletion> *completionHandlers;
1818

19-
- (void)voipRegistration;
20-
- (void)registerUserNotification:(NSDictionary *)permissions;
21-
- (NSDictionary *)checkPermissions;
2219
+ (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type;
2320
+ (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type;
24-
+ (NSString *)getCurrentAppBackgroundState;
2521
+ (void)addCompletionHandler:(NSString *)uuid completionHandler:(RNVoipPushNotificationCompletion)completionHandler;
2622
+ (void)removeCompletionHandler:(NSString *)uuid;
2723

0 commit comments

Comments
 (0)