function push_notifications_c2dm_send_message in Push Notifications 7
Send out push notifications through C2DM.
Parameters
$tokens: Array of iOS tokens
$payload: Payload to send.
Return value
Array with the following keys:
- count_attempted (# of attempted messages sent)
- count_success (# of successful sends)
- success (# boolean)
- message (Prepared result message)
2 calls to push_notifications_c2dm_send_message()
- push_notifications_mass_push_form_submit in includes/
push_notifications.admin.inc - Submit handler for sending out a mass-push notification.
- push_notifications_send_alert in ./
push_notifications.module - Handle delivery of simple alert message.
File
- ./
push_notifications.module, line 800 - Push Notifications functionality.
Code
function push_notifications_c2dm_send_message($tokens, $payload) {
if (!is_array($tokens) || empty($payload) || is_array($tokens) && empty($tokens)) {
return FALSE;
}
// Determine an updated authentication token.
// Google is very vague about how often this token changes,
// so we'll just get a new token every time.
$auth_token = push_notifications_c2dm_token();
if (!$auth_token) {
$result['message'] = t('Google C2DM Server did not provide an authentication token. Check your C2DM credentials.');
return $result;
}
// Define an array of result values.
$result = array(
'count_attempted' => 0,
'count_success' => 0,
'success' => 0,
'message' => '',
);
// Define the header.
$headers = array();
$headers[] = 'Authorization: GoogleLogin auth=' . $auth_token;
// Send a push notification to every recipient.
foreach ($tokens as $token) {
$result['count_attempted']++;
// Convert the payload into the correct format for C2DM payloads.
// Prefill an array with values from other modules first.
$data = array();
foreach ($payload as $key => $value) {
if ($key != 'alert') {
$data['data.' . $key] = $value;
}
}
// Fill the default values required for each payload.
$data['registration_id'] = $token;
$data['collapse_key'] = time();
$data['data.message'] = $payload['alert'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, PUSH_NOTIFICATIONS_C2DM_SERVER_POST_URL);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
// If Google's server returns a reply, but that reply includes an error, log the error message.
if ($info['http_code'] == 200 && (isset($response) && preg_match('/Error/', $response))) {
watchdog('push_notifications', "Google's Server returned an error: " . $response, NULL, WATCHDOG_ERROR);
// If the device token is invalid or not registered (anymore because the user
// has uninstalled the application), remove this device token.
if (preg_match('/InvalidRegistration/', $response) || preg_match('/NotRegistered/', $response)) {
push_notifications_purge_token($token, PUSH_NOTIFICATIONS_TYPE_ID_ANDROID);
watchdog('daddyhunt_apns', 'C2DM token not valid anymore. Removing token ' . $token);
}
}
// Success if the http response status is 200 and the response
// data does not containt the word "Error".
if ($info['http_code'] == 200 && (isset($response) && !preg_match('/Error/', $response))) {
$result['count_success']++;
}
}
$result['message'] = t('Successfully sent !count_success Android push messages (attempted to send !count messages).', array(
'!count_success' => $result['count_success'],
'!count' => $result['count_attempted'],
));
$result['success'] = TRUE;
// Invoke rules.
if (module_exists('rules')) {
rules_invoke_event_by_args('push_notifications_after_c2dm_send', array(
'type_id' => PUSH_NOTIFICATIONS_TYPE_ID_ANDROID,
'payload' => $payload,
'count_attempted' => $result['count_attempted'],
'count_success' => $result['count_success'],
'success' => $result['success'],
'result_message' => $result['message'],
));
}
return $result;
}