This repository was archived by the owner on Feb 27, 2023. It is now read-only.
forked from HazemKhaled/TiPushNotification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (130 loc) · 4.07 KB
/
Copy pathindex.js
File metadata and controls
153 lines (130 loc) · 4.07 KB
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
/**
* Yes you can use it with your self-hosted server or any other cloud services
* like Parse.com or Appcelerator cloud.
*
* @auther Hazem Khaled <hazem.khaled@gmail.com>
*/
var ANDROID = Ti.Platform.name === 'android',
IOS = Ti.Platform.name === 'iPhone OS' || Ti.Platform.name === 'iOS';
if (ANDROID) {
var GCM = require("nl.vanvianen.android.gcm");
}
function TiPush(e) {
this.backendUrl = e.backendUrl;
this.token = '';
this.os = (ANDROID) ? 'Android' : 'iOS';
}
TiPush.prototype.registerDevice = function(_prams) {
var that = this,
token = '',
onReceive = _prams.onReceive,
onStart = _prams.onStart || _prams.onReceive,
onResume = _prams.onResume || _prams.onReceive,
pnOptions = _prams.pnOptions,
extraOptions = _prams.extraOptions || {};
for (var key in extraOptions) {
that[key] = extraOptions[key];
}
function deviceTokenSuccess(e) {
if (ANDROID) {
Ti.API.debug('[TiPush] Device Token:', e.registrationId);
token = e.registrationId;
} else if (IOS) {
Ti.API.debug('[TiPush] Device Token:', e.deviceToken);
token = e.deviceToken;
}
that.token = token;
var uploadParams = {};
for (var key in that) {
if (['backendUrl', 'getToken', 'registerDevice'].indexOf(key) === -1) {
uploadParams[key] = that[key];
}
}
var xhr = Ti.Network.createHTTPClient({
onload: function() {
Ti.API.debug("[TiPush] Token sent to our backend");
},
onerror: function() {
Ti.API.error("[TiPush] Can't send tokend to our backend");
}
});
xhr.open("POST", that.backendUrl);
xhr.send(uploadParams);
}
function deviceTokenError(e) {
Ti.API.error('[TiPush] Token Error:', e.error);
}
function receivePush(e) {
Ti.API.debug("[TiPush] onReceive Push callback =", JSON.stringify(e));
if (IOS) {
// Reset badge
Titanium.UI.iPhone.appBadge = null;
}
onReceive(e.data);
Ti.API.debug("[TiPush] Push notification received: Module", JSON.stringify(e.data));
}
if (ANDROID) {
GCM.registerPush({
senderId: pnOptions.senderId,
notificationSettings: pnOptions.notificationSettings,
success: deviceTokenSuccess,
error: deviceTokenError,
callback: receivePush
});
/* When the app is started */
var lastData = GCM.getLastData();
if (lastData) {
onStart({
data: lastData
});
GCM.clearLastData();
}
/* And when the app is resumed */
Ti.Android.currentActivity.addEventListener("resume", function() {
var lastData = GCM.getLastData();
if (lastData) {
onResume({
data: lastData
});
GCM.clearLastData();
}
});
} else if (IOS) {
// Check if the device is running iOS 8 or later
if (parseInt(Ti.Platform.version.split(".")[0], 10) >= 8) {
function registerForPush() {
Ti.Network.registerForPushNotifications({
success: deviceTokenSuccess,
error: deviceTokenError,
callback: receivePush
});
// Remove event listener once registered for push notifications
Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
}
// Wait for user settings to be registered before registering for push notifications
Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);
// Register notification types to use
Ti.App.iOS.registerUserNotificationSettings({
types: pnOptions.types,
categories: pnOptions.categories
});
} else {
// For iOS 7 and earlier
Ti.Network.registerForPushNotifications({
// Specifies which notifications to receive
types: pnOptions.types,
success: deviceTokenSuccess,
error: deviceTokenError,
callback: receivePush
});
}
} else {
Ti.API.warn("[TiPush] Push notification not implemented yet into TiPushNotification for", Ti.Platform.osname);
}
};
TiPush.prototype.getToken = function() {
return this.token;
};
exports.init = function(param) {
return new TiPush(param);
};