This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
135 lines (117 loc) · 4.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { NativeModules,NativeEventEmitter,DeviceEventEmitter } from 'react-native';
const PushModule = NativeModules.RNBmdPushReact;
const eventEmitter = new NativeEventEmitter(NativeModules.RNBmdPushReact)
const emitterMap = new Map();
/**
* React-Native module to handle devive requests.
* @module PushNotification
*/
class PushNotification {
/**
* Creates a PushNotification instance
* @method module:PushNotification#init
* @param {Object} config The push initialise parameters like appGUID, clientSecret,region etc.
* @return The PushNotification object for calls to be linked.
*/
init(config: Object) {
config.hasOption = false;
console.log("Not Found.");
Object.keys(config).forEach(key => {
if (key === "options") {
console.log("Found.");
config.hasOption = true;
}
});
return PushModule.initialize(config);
}
/**
* Register device to IBM cloud push notifications instance
* @method module:PushNotification#register
* @param {Object} options The push register parameter like userId.
* @return Response from the server.
*/
register(options: Object) {
return PushModule.registerDevice(options);
}
/**
* Un-register device to IBM cloud push notifications instance
* @method module:PushNotification#unregisterDevice
* @return Response from the server.
*/
unregisterDevice() {
return PushModule.unregisterDevice();
}
/**
* Retrieve available tags from IBM cloud push notifications instance.
* @method module:PushNotification#retrieveAvailableTags
* @return Response from the server.
*/
retrieveAvailableTags() {
return PushModule.retrieveAvailableTags();
}
/**
* Subscribe to a tag in IBM cloud push notifications instance
* @method module:PushNotification#subscribe
* @param {string} tag The tag name.
* @return Response from the server.
*/
subscribe(tag: string) {
return PushModule.subscribe(tag);
}
/**
* Un-subscribe from a tag in IBM cloud push notifications instance
* @method module:PushNotification#unsubscribe
* @param {string} tag The tag name.
* @return Response from the server.
*/
unsubscribe(tag: string) {
return PushModule.unsubscribe(tag);
}
/**
* Retrieve all tag subscriptions of the device from IBM cloud push notifications instance
* @method module:PushNotification#retrieveSubscriptions
* @return Response from the server.
*/
retrieveSubscriptions() {
return PushModule.retrieveSubscriptions();
}
/**
* Method to listen to push events
* @method module:PushNotification#registerNotificationsCallback
* @param {string} eventListenerName Name of the push event, by default its `BMSPushRecieveListener`.
*/
registerNotificationsCallback(eventListenerName = 'BMSPushRecieveListener') {
if (typeof eventListenerName !== 'string') {
throw new Error(`${eventListenerName} is not of type string`);
}
if(!emitterMap.has(eventListenerName)) {
emitterMap.forEach(function(value, key, map) {
// value is a EmitterSubscription Object
// 'remove' is actually removing the subscription
value.remove();
})
emitterMap.set(
// key -- string
eventListenerName,
//value -- EmitterSubscription Object
eventEmitter.addListener('onBMDPushReceived', function(body) {
console.log(body);
eventEmitter.emit(eventListenerName, body);
})
);
PushModule.registerNotificationsCallback('onBMDPushReceived');
}
}
/**
* Method to listen to push status change events
* @method module:PushNotification#setNotificationStatusListener
* @param {string} eventListenerName Name of the push change event, by default its `onBMSPushStatusListener`.
*/
setNotificationStatusListener(eventListenerName = 'onBMSPushStatusListener') {
PushModule.setNotificationStatusListener(eventListenerName);
}
}
var Push = new PushNotification();
export {
Push
}