-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathurbanairship_api.module
200 lines (180 loc) · 5.94 KB
/
urbanairship_api.module
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
<?php
// $Id$
/**
* Implements hook_menu().
*/
function urbanairship_api_menu() {
$items = array();
$items['admin/settings/urbanairship_api'] = array(
'title' => 'Urban Airship API Settings',
'description' => 'Administer settings for Urban Airship.',
'page callback' => 'drupal_get_form',
'page arguments' => array('urbanairship_api_settings_form'),
'access arguments' => array('administer urbanairship_api'),
);
return $items;
}
/**
* Administrative settings for Urban Airship.
*
* @param string $form
* @return void
*/
function urbanairship_api_settings_form($form_state) {
$form = array();
$form['credentials'] = array(
'#type' => 'fieldset',
'#title' => t('Credentials'),
'#description' => t('Enter credentials for Urban Airship'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#access' => user_access('administer urbanairship_api'),
);
$form['credentials']['ua_app_master_key'] = array(
'#type' => 'textfield',
'#title' => t('App Master Key'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_app_master_key', ''),
);
$form['credentials']['ua_app_key'] = array(
'#type' => 'textfield',
'#title' => t('App Key'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_app_key', ''),
);
$form['credentials']['ua_test_device_token'] = array(
'#type' => 'textfield',
'#title' => t('Test Device Token'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_test_device_token', ''),
);
$form['connection_urls'] = array(
'#type' => 'fieldset',
'#title' => t('Connection URLS'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access('administer urbanairship_api'),
);
$form['connection_urls']['ua_server'] = array(
'#type' => 'textfield',
'#title' => t('Server'),
'#description' => t('The URL for the server.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_server', 'go.urbanairship.com'),
);
$form['connection_urls']['ua_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Base URL'),
'#description' => t('The base URL for the API.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_base_url', 'https://go.urbanairship.com/api'),
);
$form['connection_urls']['ua_device_token_url'] = array(
'#type' => 'textfield',
'#title' => t('Device Token URL'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_device_token_url', 'go.urbanairship.com/api' . '/device_tokens/'),
);
$form['connection_urls']['ua_broadcast_url'] = array(
'#type' => 'textfield',
'#title' => t('Broadcast URL'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_broadcast_url', 'go.urbanairship.com/api' . '/push/broadcast/')
);
$form['connection_urls']['ua_push_url'] = array(
'#type' => 'textfield',
'#title' => t('Push URL'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ua_push_url', 'go.urbanairship.com/api' . '/push/')
);
return system_settings_form($form);
}
/**
* Sends a push notification to specific devices.
*
* @return void
*/
function urbanairship_api_push($payload = array(), $device_tokens = NULL, $aliases = NULL, $tags = NULL, $schedule_for = NULL, $exclude_tokens = NULL) {
$data = array();
if (is_array($payload)) {
$data['aps'] = $payload;
}
else {
return;
}
if (empty($device_tokens) && empty($aliases) && empty($tags)) {
return;
}
if (!empty($device_tokens)) {
$data['device_tokens'] = (array)$device_tokens;
}
if (!empty($aliases)) {
$data['aliases'] = (array)$aliases;
}
if (!empty($tags)) {
$data['tags'] = (array)$tags;
}
if (!empty($schedule_for)) {
$data['schedule_for'] = (array)$schedule_for;
}
if (!empty($exclude_tokens)) {
$data['exclude_tokens'] = (array)$exclude_tokens;
}
$body = json_encode($data);
$push_url = variable_get('ua_push_url', 'go.urbanairship.com/api/push/');
$user = variable_get('ua_app_key', '');
$pass = variable_get('ua_app_master_key', '');
$connect = 'https://' . $user . ':' . $pass . '@' . $push_url;
$headers = array('Content-Type' => 'application/json');
$request = drupal_http_request($connect, $headers, 'POST', $body);
if ($request->code == 200) {
watchdog('urbanairship_api', 'Succesfully sent push notification for payload <pre>@payload</pre>', array('@payload' => print_r($payload, TRUE)));
return TRUE;
}
else {
watchdog('urbanairship_api', 'Push notification was not sent, received error @error.', array('@error' => $request->code), WATCHDOG_ERROR);
return FALSE;
}
}
/**
* Sends a push notification to all registered devices.
*
* @return void
*/
function urbanairship_api_broadcast($payload = array(), $exclude_tokens = NULL) {
if (!$payload) {
return;
}
if ($exclude_tokens != null) {
$payload['exclude_tokens'] = $exclude_tokens;
}
$body = json_encode(array('aps' => $payload));
$broadcast_url = variable_get('ua_broadcast_url', 'go.urbanairship.com/api/push/broadcast/');
$user = variable_get('ua_app_key', '');
$pass = variable_get('ua_app_master_key', '');
$connect = 'https://' . $user . ':' . $pass . '@' . $broadcast_url;
$headers = array('Content-Type' => 'application/json');
$request = drupal_http_request($connect, $headers, 'POST', $body);
if ($request->code == 200) {
watchdog('urbanairship_api', 'Succesfully sent push notification for payload <pre>@payload</pre>', array('@payload' => print_r($payload, TRUE)));
return TRUE;
}
else {
watchdog('urbanairship_api', 'Push notification was not sent, received error @error.', array('@error' => $request->code), WATCHDOG_ERROR);
return FALSE;
}
}
/**
* Implements hook_perm().
*/
function urbanairship_api_perm() {
return array('administer urbanairship_api');
}