I have successfully implemented the android push notification using google c2dm. I always send a post request for a device, and one device delay 1-2 seconds. So, if I have 1000 devices, my script will need more than 1000 seconds to finish the push to all devices.
The thing I want to know is, can we send the post request for all devices to google c2dm? If we can, how to do?
I'm using PHP script.
Here is my code to push a message to a device:
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText, $infoType, $messageInfo) {
$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText,
'data.type' => $infoType,
'data.data' => $messageInfo
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
If I have more devices I loop it like this:
while($row = mysql_fetch_assoc($result)) {
sendMessageToPhone($authCode, $row['deviceRegistrationId'], GOOGLE_MSG_TYPE, $messageText, $infoType, $messageInfo);
}
Thank for helping.
The authentification is the most expansive (in time) action in the all process, that probably why you have a 1 second delay between each send.
To speed up the process , you should not authenticate each time.
Simply auth once ,and get the Auth token. This token has a certain TTL but nothing is specified by Google.
Then loop over your devices, and send using the previous auth token. The auth token can change (rarely) and can be found in the response header Update-Client-Auth
.
The all process shouldn't take more the few hundred ms by device.
Also consider using stream instead of curl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With