-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
170 lines (133 loc) · 4.9 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Created by lvbingru on 10/23/15.
*/
import {NativeModules, DeviceEventEmitter} from 'react-native';
const nativeModule = NativeModules.JPush;
const invariant = require('invariant');
const _notifHandlers = [];
let _initialNotification = nativeModule &&
nativeModule.initialNotification;
export const JpushEventReceiveMessage = 'kJPFNetworkDidReceiveMessageNotification'
export const JpushEventOpenMessage = 'kJPFNetworkDidOpenMessageNotification'
export const JpushEventReceiveCustomMessage = 'kJPFNetworkDidReceiveCustomMessageNotification'
export default class JPushNotification {
_data;
constructor(nativeNotif) {
this._data = {};
if (typeof nativeNotif === 'string') {
nativeNotif = JSON.parse(nativeNotif)
}
if (nativeNotif) {
this._data = nativeNotif
}
}
static popInitialNotification() {
const initialNotification = _initialNotification &&
new JPushNotification(_initialNotification);
_initialNotification = null;
return initialNotification;
}
static setAlias(alias){
if (!alias) {
alias = ''
}
nativeModule.setAlias(alias)
}
static setTags(tags, alias){
if (!alias) {
alias = ''
}
nativeModule.setTags(tags, alias)
}
static getRegistrationID(){
return new Promise(resolve=>{
nativeModule.getRegistrationID(resolve)
})
}
static addEventListener(type: string, handler: Function) {
checkListenerType(type)
if (type === JpushEventOpenMessage && _initialNotification) {
handler(this.popInitialNotification())
}
const listener = DeviceEventEmitter.addListener(
type,
(note) => {
handler(note && new JPushNotification(note));
}
);
_notifHandlers.push(listener)
return listener;
}
static removeEventListener(listener) {
const index = _notifHandlers.indexOf(listener)
if (index >=0) {
_notifHandlers.splice(index,1)
listener.remove()
}
}
// ios only
static resetBadge(){
nativeModule.resetBadge && nativeModule.resetBadge()
}
static requestPermissions(permissions){
nativeModule.requestPermissions && nativeModule.requestPermissions(permissions)
}
static abandonPermissions(){
nativeModule.abandonPermissions && nativeModule.abandonPermissions()
}
static cancelAllLocalNotifications(){
nativeModule.cancelAllLocalNotifications && nativeModule.cancelAllLocalNotifications()
}
static setLocalNotification(notification){
nativeModule.setLocalNotification && nativeModule.setLocalNotification(notification)
}
static beginLogPageView(page, duration){
nativeModule.beginLogPageView && nativeModule.beginLogPageView(page, duration)
}
static setLogOFF(){
nativeModule.setLogOFF && nativeModule.setLogOFF()
}
static crashLogON(){
nativeModule.crashLogON && nativeModule.crashLogON()
}
static setLocation(location){
nativeModule.setLocation && nativeModule.setLocation(location.latitude, location.longitude)
}
static startLogPageView(page){
nativeModule.startLogPageView && nativeModule.startLogPageView(page)
}
static stopLogPageView(page){
nativeModule.stopLogPageView && nativeModule.stopLogPageView(page)
}
// android only
static clearNotificationById(notificationId : number){
nativeModule.clearNotificationById && nativeModule.clearNotificationById(notificationId)
}
static clearAllNotifications(){
nativeModule.clearAllNotifications && nativeModule.clearAllNotifications()
}
static clearLocalNotifications(){
nativeModule.clearLocalNotifications && nativeModule.clearLocalNotifications()
}
static stopPush(){
nativeModule.stopPush && nativeModule.stopPush()
}
static resumePush(){
nativeModule.resumePush && nativeModule.resumePush()
}
static setLatestNotificationNumber(maxNum : number){
nativeModule.setLatestNotificationNumber && nativeModule.setLatestNotificationNumber(maxNum)
}
static setPushTime(weaks : array, startHour : number, endHour : number){
nativeModule.setPushTime && nativeModule.setPushTime(weaks, startHour, endHour)
}
static setSilenceTime(startHour, startMinute, endHour, endMinute) {
nativeModule.setSilenceTime && nativeModule.setSilenceTime(startHour, startMinute, endHour, endMinute)
}
}
function checkListenerType(type) {
invariant(
type === JpushEventReceiveMessage || type === JpushEventOpenMessage || type === JpushEventReceiveCustomMessage,
'JPushNotification only supports `JpushEventReceiveMessage` ,`JpushEventOpenMessage`, `JpushEventReceiveCustomMessage`, events'
);
}