This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
226 lines (194 loc) · 5.23 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
/**
* 信鸽推送
* Created by Jeepeng on 16/8/3.
*/
import {
Platform,
NativeModules,
NativeEventEmitter,
} from 'react-native';
let { XGPushManager } = NativeModules;
let XGNativeEventEmitter = new NativeEventEmitter(XGPushManager);
let _handlers = new Map();
const EventMapping = Platform.select({
android: {
register: 'remoteNotificationsRegistered',
notification: 'remoteNotificationReceived',
localNotification: 'localNotificationReceived',
message: 'messageReceived'
},
ios: {
register: 'remoteNotificationsRegistered',
notification: 'remoteNotificationReceived',
localNotification: 'localNotificationReceived',
},
});
class XGPush {
static init(accessId, accessKey) {
let accessIdNum = Number(accessId);
if (isNaN(accessIdNum)) {
console.error(`[XGPush init] accessId is not a number!`);
} else {
if (Platform.OS === 'ios') {
XGPushManager.startXGWithAppID(accessIdNum, accessKey);
} else {
XGPushManager.init(accessIdNum, accessKey);
}
}
}
static register(account) {
if (Platform.OS === 'ios') {
!!account && XGPushManager.bindWithAccount(account);
return XGPushManager.requestPermissions({
alert: true,
badge: true,
sound: true
});
} else {
if (account) {
return XGPushManager.bindAccount(account);
} else {
return XGPushManager.registerPush();
}
}
}
static setTag(tagName) {
if (Platform.OS === 'ios') {
return XGPushManager.bindWithTag(tagName);
} else {
return XGPushManager.setTag(tagName);
}
}
static deleteTag(tagName) {
if (Platform.OS === 'ios') {
return XGPushManager.unbindWithTag(tagName);
} else {
return XGPushManager.deleteTag(tagName);
}
}
static unRegister() {
if (Platform.OS === 'ios') {
return XGPushManager.stopXGNotification();
} else {
return XGPushManager.unregisterPush();
}
}
static setApplicationIconBadgeNumber(number) {
XGPushManager.setApplicationIconBadgeNumber(number);
}
static getApplicationIconBadgeNumber(callback) {
XGPushManager.getApplicationIconBadgeNumber(callback);
}
static checkPermissions(callback) {
if (Platform.OS === 'ios') {
return XGPushManager.checkPermissions(callback);
}
}
static getInitialNotification() {
return XGPushManager.getInitialNotification();
}
static onLocalNotification(callback) {
this.addEventListener('localNotification', callback)
}
/**
* 透传消息 Android only
*/
static onMessage(callback) {
if (Platform.OS === 'android') {
this.addEventListener('message', callback)
}
}
static addEventListener(eventType, callback) {
let event = EventMapping[eventType];
if (!event) {
console.warn('XGPush only supports `notification`, `register` and `localNotification` events');
return;
}
let listener = XGNativeEventEmitter.addListener(event, (data) => {
let result = data;
if (eventType === 'register') {
result = data['deviceToken'];
}
callback(result);
});
_handlers.set(callback, listener);
}
static removeEventListener(eventType, callback) {
if (!EventMapping[eventType]) {
console.warn('XGPush only supports `notification`, `register` and `localNotification` events');
return;
}
let listener = _handlers.get(callback);
if (listener) {
listener.remove();
_handlers.delete(callback);
}
}
static enableDebug(isDebug = true) {
if (Platform.OS === 'ios') {
XGPushManager.setEnableDebug(isDebug);
} else {
XGPushManager.enableDebug(isDebug);
}
}
static isEnableDebug() {
return XGPushManager.isEnableDebug();
}
/**************************** android only ************************/
/**
* 获取设备的token,只有注册成功才能获取到正常的结果
*/
static getToken() {
if (Platform.OS === 'android') {
return XGPushManager.getToken(isEnable);
} else {
return Promise.resolve();
}
}
/**
* 设置上报通知栏是否关闭 默认打开
*/
static setReportNotificationStatusEnable() {
if (Platform.OS === 'android') {
XGPushManager.setReportNotificationStatusEnable();
}
}
/**
* 设置上报APP 列表,用于智能推送 默认打开
*/
static setReportApplistEnable() {
if (Platform.OS === 'android') {
XGPushManager.setReportApplistEnable();
}
}
/**
* 打开第三方推送(在 registerPush 之前调用)
* @param isEnable
*/
static enableOtherPush(isEnable = true) {
if (Platform.OS === 'android') {
XGPushManager.enableOtherPush(isEnable);
}
}
/**
* 打开华为通道debug模式
* @param isDebug
*/
static setHuaweiDebug(isDebug = true) {
if (Platform.OS === 'android') {
XGPushManager.setHuaweiDebug(isDebug);
}
}
static initXiaomi(appId, appKey) {
if (Platform.OS === 'android') {
XGPushManager.initXiaomi(appId, appKey);
}
}
static initMeizu(appId, appKey) {
if (Platform.OS === 'android') {
XGPushManager.initMeizu(appId, appKey);
}
}
/**************************** ios only ************************/
}
export default XGPush;