forked from globocom/react-native-ua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (121 loc) · 3.78 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
import {
DeviceEventEmitter,
NativeAppEventEmitter,
NativeModules,
Platform
} from 'react-native';
const ReactNativeUAIOS = NativeModules.ReactNativeUAIOS;
const ReactNativeUAAndroid = NativeModules.ReactNativeUAAndroid;
let bridge = null;
let notification_listeners = [];
let call_notification_listeners = function (notification) {
var i = notification_listeners.length - 1;
for (i; i >= 0; i--) {
notification_listeners[i]({
'platform': notification.platform,
'event': notification.event,
'alert': notification.alert,
'data': notification.data,
'url': notification.url
});
}
}
switch (Platform.OS) {
case 'ios':
bridge = ReactNativeUAIOS;
NativeAppEventEmitter.addListener('receivedNotification', (notification) => {
var action_url = notification.data['^u'] || false;
if (notification && notification.data && notification.data.aps && notification.data.aps.alert) {
call_notification_listeners({
'platform': 'ios',
'event': notification.event,
'alert': notification.data.aps.alert,
'data': notification.data,
'url': action_url
});
}
});
break;
case 'android':
bridge = ReactNativeUAAndroid;
DeviceEventEmitter.addListener('receivedNotification', (notification) => {
var actions_json = notification['com.urbanairship.actions'] || false;
var actions = actions_json ? JSON.parse(actions_json) : false;
var action_url = actions ? actions['^u'] || false : false;
call_notification_listeners({
'platform': 'android',
'event': notification.event,
'alert': notification['com.urbanairship.push.ALERT'],
'data': notification,
'url': action_url
});
});
break;
}
class ReactNativeUA {
static enable_notification () {
bridge.enableNotification();
}
static disable_notification () {
bridge.disableNotification();
}
static enable_geolocation () {
bridge.enableGeolocation();
}
static enable_action_url () {
bridge.enableActionUrl();
}
static disable_action_url () {
bridge.disableActionUrl();
}
static handle_background_notification () {
bridge.handleBackgroundNotification();
}
static add_tag (tag) {
bridge.addTag(tag);
}
static remove_tag (tag) {
bridge.removeTag(tag);
}
/*
* @param {Object} time
* @param {number} time.startHour
* @param {number} time.startMinute
* @param {number} time.endHour
* @param {number} time.endMinute
*/
static set_quiet_time (time) {
bridge.setQuietTime(time);
}
static set_quiet_time_enabled (enabled) {
bridge.setQuietTimeEnabled(enabled);
}
/*
iOS only.
*/
static are_notifications_enabled (callback) {
return new Promise((resolve, reject) => {
bridge.areNotificationsEnabled(enabled => {
callback && callback(null, enabled);
resolve(enabled);
})
})
}
static set_named_user_id (nameUserId) {
bridge.setNamedUserId(nameUserId);
}
static on_notification (callback) {
notification_listeners.push(callback);
}
static set_android_small_icon(iconName) {
if (Platform.OS === 'android') {
bridge.setAndroidSmallIcon(iconName);
}
}
static set_android_large_icon (iconName) {
if (Platform.OS === 'android') {
bridge.setAndroidLargeIcon(iconName);
}
}
}
export default ReactNativeUA