forked from wonday/react-native-aliyun-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (144 loc) · 4.66 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
/**
* Copyright (c) 2017-present, Wonday (@wonday.org)
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
import React,{ Component } from 'react';
import {
requireNativeComponent,
Platform,
} from 'react-native';
const AliyunPushNative = require('react-native').NativeModules.AliyunPush;
const { NativeEventEmitter } = require('react-native');
const LocalEventEmitter = new NativeEventEmitter(AliyunPushNative);
var listeners = {};
var id = 0;
var META = '__listener_id';
function getKey(listener,type){
if (!listener.hasOwnProperty(META)){
if (!Object.isExtensible(listener)) {
return 'F';
}
Object.defineProperty(listener, META, {
value: 'L' + type + ++id,
});
}
return listener[META];
};
export default class AliyunPush {
static getDeviceId = () => {
return AliyunPushNative.getDeviceId();
}
static getInitialMessage = () => {
return AliyunPushNative.getInitialMessage().then(e => {
if(e && e.extraStr) {
let extras = JSON.parse(e.extraStr);
if (extras) {
if (extras.badge) {
let badgeNumber = parseInt(extras.badge);
if (!isNaN(badgeNumber)) {
AliyunPush.setApplicationIconBadgeNumber(badgeNumber);
}
}
e.extras = extras;
}
delete e.extraStr;
}
return e;
});
}
static getApplicationIconBadgeNumber = (callback) => {
AliyunPushNative.getApplicationIconBadgeNumber(function(args) {
callback(args);
});
}
static setApplicationIconBadgeNumber = (num) => {
AliyunPushNative.setApplicationIconBadgeNumber(num);
}
static syncBadgeNum = (num) => {
if(Platform.OS === 'android') {
return;
}
AliyunPushNative.syncBadgeNum(num);
}
static bindAccount = (account) => {
return AliyunPushNative.bindAccount(account);
}
static unbindAccount = () => {
return AliyunPushNative.unbindAccount();
}
static bindTag = (target, tags, alias) => {
return AliyunPushNative.bindTag(target, tags, alias);
}
static unbindTag = (target, tags, alias) => {
return AliyunPushNative.unbindTag(target, tags, alias);
}
static listTags = (target) => {
return AliyunPushNative.listTags(target);
}
static addAlias = (alias) => {
return AliyunPushNative.addAlias(alias);
}
static removeAlias = (alias) => {
return AliyunPushNative.removeAlias(alias);
}
static listAliases = () => {
return AliyunPushNative.listAliases();
}
static getAuthorizationStatus = (callback) => {
if(Platform.OS === 'android') {
// android always return true
callback(true);
} else {
AliyunPushNative.getAuthorizationStatus(function(args) {
callback(args);
});
}
}
static addListener = (callback) => {
AliyunPush._addListener(callback,"aliyunPushReceived");
};
static removeListener = (callback) => {
AliyunPush._removeListener(callback, "aliyunPushReceived");
};
static removeAllListeners = () => {
for(var key in listeners){
listeners[key].remove();
listeners[key] = null;
}
};
static _addListener = (callback,type) => {
var key = getKey(callback,type);
listeners[key] = LocalEventEmitter.addListener(type,
(e) => {
// convert json string to obj
if (e.extraStr) {
let extras = JSON.parse(e.extraStr);
if (extras) {
if (extras.badge) {
let badgeNumber = parseInt(extras.badge);
if (!isNaN(badgeNumber)) {
AliyunPush.setApplicationIconBadgeNumber(badgeNumber);
}
}
e.extras = extras;
}
delete e.extraStr;
callback(e);
} else {
callback(e);
}
});
};
static _removeListener = (callback,type) => {
var key = getKey(callback,type);
if (!listeners[key]) {
return;
}
listeners[key].remove();
listeners[key] = null;
};
}