-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (150 loc) · 3.28 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
// main index.js
import {
NativeModules,
Platform,
NativeEventEmitter,
PermissionsAndroid,
Alert,
} from 'react-native';
const RNDuxPush = NativeModules.RNDuxPush;
import PushNotificationIOS from '@react-native-community/push-notification-ios';
class DuxPush extends NativeEventEmitter {
// 构造
constructor(props) {
super(RNDuxPush);
// 初始状态
this.state = {};
}
init(appkey, secret, data) {
if (Platform.OS == 'android') {
RNDuxPush.init(appkey, secret, data);
} else {
RNDuxPush.init(data?.umeng, data?.channel || 'duxapp');
}
}
/**
* 判断当前是否打开了通知权限
* @returns
*/
notificationsEnabled() {
return RNDuxPush.notificationsEnabled();
}
/**
* 去打开通知
*/
goPushSetting() {
RNDuxPush.goPushSetting();
}
/**
* 设置别名
* @param text
*/
setAlias(text) {
return RNDuxPush.setAlias(text);
}
/**
* 注销别名
* @param text
*/
unsetAlias(text) {
return RNDuxPush.unsetAlias(text);
}
/**
* 设置标签
* @param text
*/
setTag(text) {
return RNDuxPush.setTag(text);
}
/**
* 注销标签
* @param text
*/
unsetTag(text) {
return RNDuxPush.unsetTag(text);
}
notify(title, body) {
if (Platform.OS == 'ios') {
PushNotificationIOS.addNotificationRequest({
id: '1',
title: title,
body: body,
});
} else {
RNDuxPush.notify(title, body);
}
}
/**
*
* @param type
* ios :
* notification => 监听收到apns通知
* localNotification => 监听收到本地通知
* register => 注册deviceToken 通知
*
* android :
* duxpush_notify => 监听收到推送
* duxpush_click => 监听推送被点击
* @param handler
*/
addEventListener(type, handler) {
if (Platform.OS == 'ios') {
switch (type) {
case 'notification':
case 'duxpush_notify':
if (type == 'duxpush_notify') {
type = 'notification';
}
PushNotificationIOS.addEventListener(type, (res) => {
handler && handler(res._data);
});
break;
case 'localNotification':
case 'register':
PushNotificationIOS.addEventListener(type, handler);
break;
default:
this.addListener(type, handler);
break;
}
} else {
this.addListener(type, (res) => {
handler && handler(JSON.parse(res));
});
}
}
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);
}
}
/**
* 通过点击通知启动app
* @param handler
*/
getInitialNotification(handler) {
if (Platform.OS == 'ios') {
PushNotificationIOS.getInitialNotification()
.then((res) => {
if (res && res._data) {
handler && handler(res._data);
}
});
} else {
// RNDuxPush.getInitialNotification()
// .then(handler);
}
}
}
module.exports = new DuxPush();