-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc-push.html
More file actions
315 lines (270 loc) · 9.68 KB
/
sc-push.html
File metadata and controls
315 lines (270 loc) · 9.68 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
The complete set of authors may be found at http://polymer.github.io/AUTHORS
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
-->
<link rel="import" href="../polymer/polymer.html" />
<script>
/* eslint-disable */
(function () {
var CapacitorPush = ((window.Capacitor || {}).Plugins || {}).PushNotifications;
window.Polymer({
is: 'sc-push',
properties: {
/**
* A string that is used for GCM registration on Android
*
* @default '1076345567071'
*/
gcmSenderId: {
type: String,
value: '1076345567071' // Parse.com default GCM Sender ID
},
/**
* An object that determines default values for `config`
*
*/
defaultConfig: {
type: Object,
readOnly: true,
value: {
ios: {
alert: true,
badge: true,
sound: true
},
android: {
clearBadge: true,
icon: 'notification'
},
windows: {}
}
},
/**
* An object to configure various parameters on the push notificaiton layer
* Refer to: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md
*
* @default '{}'
*/
config: {
type: Object
},
/**
* A boolean to enable logs to be written to `console.log`
*
* @default false
*/
debug: {
type: Boolean,
value: false
},
/**
* A string that contains the registrationId/deviceId after successful registration
*
* @default '1076345567071'
*/
registrationId: {
type: String,
value: undefined,
notify: true
},
/**
* A string that contains the state of: 'init|success|error'
*
* @default 'init'
*/
state: {
type: String,
value: 'init'
}
},
log: function () {
return this.debug && window.console.log.apply(window.console, arguments);
},
init: function () {
if (CapacitorPush) {
if (this.initialized) {
return;
}
this.initialized = true;
// Remove any existing listeners to prevent duplicates
if (CapacitorPush.removeAllListeners) {
CapacitorPush.removeAllListeners();
}
// Add listeners BEFORE registering
CapacitorPush.addListener('registration', function (token) {
this.log('Push registration event received', token);
this.onRegistration({ registrationId: token.value });
}.bind(this));
CapacitorPush.addListener('registrationError', function (error) {
this.log('Push registration error event received', error);
this.onError({ message: JSON.stringify(error) });
}.bind(this));
CapacitorPush.addListener('pushNotificationReceived', function (notification) {
this.onNotification(notification);
}.bind(this));
CapacitorPush.addListener('pushNotificationActionPerformed', function (payload) {
this.onNotification(payload.notification);
}.bind(this));
// Register for push notifications
if (CapacitorPush.checkPermissions) {
// Capacitor 3+ API
CapacitorPush.checkPermissions()
.then(function (permStatus) {
this.log('Current permission status:', permStatus);
if (permStatus.receive === 'prompt') {
return CapacitorPush.requestPermissions();
}
return permStatus;
}.bind(this))
.then(function (permStatus) {
this.log('Permission status after request:', permStatus);
if (permStatus.receive !== 'granted') {
throw new Error('User denied push notification permissions!');
}
this.log('Registering for push notifications...');
return CapacitorPush.register();
}.bind(this))
.then(function () {
this.log('Push registration initiated successfully');
}.bind(this))
.catch(function (err) {
this.log('Push registration error:', err);
this.onError({ message: JSON.stringify(err) });
}.bind(this));
}
else if (CapacitorPush.requestPermission) {
// Capacitor 2 API (singular method name)
CapacitorPush.requestPermission()
.then(function (permStatus) {
this.log('Permission granted:', permStatus);
this.log('Registering for push notifications...');
CapacitorPush.register();
}.bind(this))
.catch(function (err) {
this.log('Push registration error:', err);
this.onError({ message: JSON.stringify(err) });
}.bind(this));
}
else if (CapacitorPush.requestPermissions) {
// Capacitor 3+ API (plural method name, fallback)
CapacitorPush.requestPermissions()
.then(function (permStatus) {
this.log('Permission granted:', permStatus);
if (permStatus.receive === 'granted') {
this.log('Registering for push notifications...');
CapacitorPush.register();
} else {
throw new Error('User denied push notification permissions!');
}
}.bind(this))
.catch(function (err) {
this.log('Push registration error:', err);
this.onError({ message: JSON.stringify(err) });
}.bind(this));
}
else {
// Fallback: try to register directly
this.log('Registering for push notifications (no permission check)...');
try {
CapacitorPush.register();
} catch (err) {
this.log('Push registration error:', err);
this.onError({ message: JSON.stringify(err) });
}
}
}
else if (!window.PushNotification) {
this._fails = (this._fails || 0) + 1;
if (this._fails > 10) {
this._fails = 0;
return;
}
return setTimeout(this.init.bind(this), 1000);
}
else {
var config = Object.assign({}, this.config);
// Set defaults
config.android = config.android || {};
['ios', 'android', 'windows'].forEach(function (platform) {
config[platform] = config[platform] || {};
Object.keys(this.defaultConfig[platform]).forEach(function (key) {
if (typeof config[platform][key] === 'undefined') {
config[platform][key] = this.defaultConfig[platform][key];
}
}.bind(this));
}.bind(this));
if (typeof config.android.senderID === 'undefined' && this.gcmSenderId) {
config.android.senderID = this.gcmSenderId;
}
this._push = window.PushNotification && window.PushNotification.init(config);
this._push.on('registration', this.onRegistration.bind(this));
this._push.on('notification', this.onNotification.bind(this));
this._push.on('error', this.onError.bind(this));
}
},
onRegistration: function (e) {
this.set('state', 'success');
this.log('Successfully registered for push notifications', e);
if (e.registrationId) {
this.set('registrationId', e.registrationId);
}
this.fire('registration', e, { bubbles: false });
},
onNotification: function (e) {
this.log('Push notification received', e);
if (CapacitorPush) {
CapacitorPush.removeAllDeliveredNotifications();
}
this.fire('notification', e, { bubbles: false });
},
onError: function (e) {
this.set('state', 'error');
this.log(e.message);
this.fire('error', e, { bubbles: false });
},
unregister: function () {
var success = this.log.bind(this, 'Successfully unregistered for push notifications');
var error = this.log.bind(this, 'Error while unregistering for push notifications');
this.set('registrationId', undefined);
this.set('state', 'init');
if (CapacitorPush) {
CapacitorPush.removeAllDeliveredNotifications().then(success).catch(error);
CapacitorPush.removeAllListeners();
}
return this._push && this._push.unregister(success, error);
},
setApplicationIconBadgeNumber: function (count) {
var success = this.log.bind(this, 'Successfully set icon badge');
var error = function () {};
if (CapacitorPush && count === 0) {
return CapacitorPush.removeAllDeliveredNotifications().then(success).catch(error);
}
return this._push && this._push.setApplicationIconBadgeNumber(success, error, count);
},
getApplicationIconBadgeNumber: function (success) {
if (CapacitorPush) {
return CapacitorPush
.getDeliveredNotifications()
.then(function (payload) {
success(((payload || {}).notifications || []).length);
})
.catch(function () {
success(0);
});
}
if (!this._push) {
return success(null);
}
return this._push.getApplicationIconBadgeNumber(success, function () {});
},
clearApplicationIconBadgeNumber: function () {
return this.setApplicationIconBadgeNumber(0);
}
});
})();
</script>