-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
248 lines (191 loc) · 4.74 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Created by zhangzy on 16/7/27.
*/
'use strict';
import {
NativeModules,
Platform,
NativeEventEmitter,
PermissionsAndroid,
} from 'react-native';
const MIPushModule = NativeModules.MIPushModule;
import PushNotificationIOS from '@react-native-community/push-notification-ios'
/**
* 获取app的版本名称\版本号和渠道名
*/
class MIPush extends NativeEventEmitter {
// 构造
constructor(props) {
super(MIPushModule);
// 初始状态
this.state = {};
}
init(appid, appkey) {
if (Platform.OS == 'android') {
MIPushModule.init(appid, appkey);
} else {
MIPushModule.initPush();
}
}
/**
* 设置别名
* @param text
*/
setAlias(text) {
MIPushModule.setAlias(text);
}
/**
* 注销别名
* @param text
*/
unsetAlias(text) {
MIPushModule.unsetAlias(text);
}
/**
* 设置主题,类似tag
* @param text
*/
subscribe(text) {
MIPushModule.subscribe(text);
}
/**
* 注销主题
* @param text
*/
unsubscribe(text) {
MIPushModule.unsubscribe(text);
}
/**
* 设置账号,一个账号需要多台设备接收通知
* @param text
*/
setAccount(text) {
MIPushModule.setAccount(text);
}
/**
* 注销账号
* @param text
*/
unsetAccount(text) {
MIPushModule.unsetAccount(text);
}
enablePush() {
if (Platform.OS == 'android') {
MIPushModule.enablePush();
}
}
disablePush() {
if (Platform.OS == 'android') {
MIPushModule.disablePush();
}
}
/**
*
* @param type
* ios :
* notification => 监听收到apns通知
* localNotification => 监听收到本地通知
* register => 注册deviceToken 通知
*
* android :
* xmpush_notify => 监听收到推送
* xmpush_click => 监听推送被点击
* xmpush_message => 监听收到透传消息
* @param handler
*/
addEventListener(type, handler) {
if (Platform.OS == 'ios') {
switch (type) {
case 'notification':
case 'localNotification':
case 'register':
PushNotificationIOS.addEventListener(type, handler);
break;
default:
this.addListener(type, handler);
break;
}
} else {
this.addListener(type, handler);
}
}
removeEventListener(type) {
if (Platform.OS == 'ios') {
switch (type) {
case 'notification':
case 'localNotification':
case 'register':
PushNotificationIOS.removeEventListener(type);
break;
default:
this.removeListener(type);
break;
}
} else {
this.removeListener(type);
}
}
/**
* 发送一个本地通知
* @param notification
*/
presentLocalNotification(notification) {
if (Platform.OS == 'ios') {
PushNotificationIOS.presentLocalNotification({
alertBody: notification.alertBody,
alertAction: '查看',
category: 'push',
userInfo: notification.userInfo,
});
} else {
MIPushModule.presentLocalNotification({});
}
}
/**
* 清除指定通知
* @param notifyId
* ios : userInfo
* android : id
*/
clearNotification(notifyId) {
if (Platform.OS == 'ios') {
PushNotificationIOS.cancelLocalNotifications(notifyId);
} else {
MIPushModule.clearNotification(notifyId);
}
}
/**
* 清除所有通知
*/
clearNotifications() {
if (Platform.OS == 'ios') {
PushNotificationIOS.cancelAllLocalNotifications();
} else {
MIPushModule.clearAllNotification();
}
}
/**
* 设置角标,仅支持ios
* @param num
*/
setBadgeNumber(num) {
if (Platform.OS == 'ios') {
PushNotificationIOS.setApplicationIconBadgeNumber(num);
}
}
/**
* 通过点击通知启动app
* @param handler
*/
getInitialNotification(handler) {
if (Platform.OS == 'ios') {
PushNotificationIOS.getInitialNotification()
.then(handler);
} else {
MIPushModule.getInitialNotification()
.then(handler);
}
}
}
MIPush = new MIPush();
module.exports = MIPush;