-
Notifications
You must be signed in to change notification settings - Fork 10
/
push_notifications.js
125 lines (114 loc) · 3.5 KB
/
push_notifications.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
var push = null;
function push_notifications_register() {
// Initializes the plugin.
push = PushNotification.init(drupalgap.settings.push_notifications);
// Set up the registration, notification and error handlers.
push.on('registration', function(data) {
push_notifications_register_device_token(data.registrationId);
});
// Handle the receipt of a notification.
push.on('notification', function(data) {
module_invoke_all('push_notifications_receive', data);
});
push.on('error', function(e) { drupalgap_alert(e.message); });
}
/**
* Implements hook_services_postprocess().
*/
function push_notifications_services_postprocess(options, result) {
try {
if (drupalgap.settings.mode != 'phonegap') { return; }
// When a user is connected and is allowed to register a token, do it.
if (options.service == 'system' && options.resource == 'connect' && user_access('register device token')) {
push_notifications_register();
}
}
catch (error) {
console.log('push_notifications_services_postprocess - ' + error);
}
}
/**
* Implements hook_services_preprocess().
*/
function push_notifications_services_preprocess(options, result) {
try {
if (drupalgap.settings.mode != 'phonegap') { return; }
// When a user logs out and is allowed to delete a token, do it.
if (options.service == 'user' && options.resource == 'logout' && user_access('remove device token')) {
push_notifications_delete_device_token();
}
}
catch (error) {
console.log('push_notifications_services_preprocess - ' + error);
}
}
function push_notifications_register_device_token(token) {
var push_token = localStorage.getItem('push_notifications_token');
if (push_token === null || push_token != token) {
var data = {
'token': token,
'type': push_notifications_platform_token(device.platform)
};
// give other modules a chance to react to registering a push notification
module_invoke_all('push_notifications_register');
push_notifications_create(data, {
success: function(result) {
if (result['success'] == 1) {
localStorage.setItem("push_notifications_token", token);
}
}
});
}
}
function push_notifications_delete_device_token() {
var push_token = localStorage.getItem('push_notifications_token');
if (push_token != null) {
push_notifications_delete(push_token, {
success: function(result) {
if (result['success'] == 1) {
window.localStorage.removeItem("push_notifications_token");
}
}
});
}
}
function push_notifications_create(data, options) {
try {
options.method = 'POST';
options.path = 'push_notifications';
options.service = 'push_notifications';
options.resource = 'push_notifications';
options.data = JSON.stringify(data);
Drupal.services.call(options);
}
catch (error) {
console.log('push_notifications_create - ' + data);
}
}
function push_notifications_delete(token, options) {
try {
options.method = 'DELETE';
options.path = 'push_notifications/' + token;
options.service = 'push_notifications';
options.resource = 'push_notifications';
Drupal.services.call(options);
}
catch (error) {
console.log('push_notifications_delete - ' + token);
}
}
function push_notifications_platform_token(platform) {
var token;
switch (platform) {
case "iOS":
token = 'ios';
break;
case "Android":
token = 'android';
break;
default:
token = null;
break;
}
return token;
}