forked from sunlu/cordova-plugin-xgpush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgpush.js
116 lines (95 loc) · 3.84 KB
/
xgpush.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
var exec = require('cordova/exec'),
channel = require('cordova/channel'),
utils = require('cordova/utils');
channel.createSticky('onCordovaXGPushReady');
channel.waitForInitialization('onCordovaXGPushReady');
function XGPush() {
var me = this;
this.channels = {
'click': channel.create('click'),
'message': channel.create('message'),
'register': channel.create('register'),
'unRegister': channel.create('unRegister'),
'show': channel.create('show'),
'deleteTag': channel.create('deleteTag'),
'setTag': channel.create('setTag'),
};
this.on = function (type, func) {
if (type in me.channels) {
me.channels[type].subscribe(func);
}
};
this.un = function (type, func) {
if (type in this.channels) {
me.channels[type].unsubscribe(func);
}
};
this.registerPush = function (account, successCallback, errorCallback) {
exec(successCallback, errorCallback, "XGPush", "registerPush", [account]);
};
this.unRegisterPush = function (successCallback, errorCallback) {
exec(successCallback, errorCallback, "XGPush", "unRegisterPush", []);
};
this.setTag = function (tagName) {
exec(null,null, "XGPush", "setTag", [tagName]);
};
this.deleteTag = function (tagName) {
exec(null, null, "XGPush", "deleteTag", [tagName]);
};
this.addLocalNotification = function (type, title, content, successCallback, errorCallback) {
exec(successCallback, errorCallback, "XGPush", "addLocalNotification", [type, title, content]);
};
this.enableDebug = function (debugMode, successCallback, errorCallback) {
exec(successCallback, errorCallback, "XGPush", "enableDebug", [debugMode]);
};
this.getLaunchInfo = function (successCallback) {
exec(function(event){
if(event.customContent&&typeof event.customContent ==="string"){
try {
var objs=JSON.parse(event.customContent);
event.customContent=objs;
event.customContent=Object.assign(...objs);
} catch (error) {
}
}
successCallback(event);
}, null, "XGPush", "getLaunchInfo", []);
};
this.getToken = function (successCallback) {
exec(successCallback, null, "XGPush", "getToken", []);
};
this.setAccessInfo = function (accessId, accessKey, successCallback, errorCallback) {
exec(successCallback, errorCallback, "XGPush", "setAccessInfo", [accessId, accessKey]);
};
this.stopNotification = function () {
exec(null, null, "XGPush", "stopNotification", []);
};
channel.onCordovaReady.subscribe(function () {
exec(
function (event) {
console.log("[XGPush] Event = " + event.type + ": ", event);
if (event && (event.type in me.channels)) {
//格式化自定义数据集
if(event.customContent&&typeof event.customContent ==="string"){
try {
var objs=JSON.parse(event.customContent);
event.customContent=objs;
event.customContent=Object.assign(...objs);
} catch (error) {
}
}
me.channels[event.type].fire(event);
}
},
null, "XGPush", "addListener", []
);
me.registerPush(null, function (info) {
console.log("[XGPush] RegisterPush: ", info);
channel.onCordovaXGPushReady.fire();
}, function (e) {
console.log("[ERROR] RegisterPush: ", e);
channel.onCordovaXGPushReady.fire();
});
});
}
module.exports = new XGPush();