android使用google c2dm一次向许多设备推送通知
我已经使用 google c2dm 成功实现了 android 推送通知。我总是发送一个设备的post请求,一个设备延迟1-2秒。因此,如果我有 1000 个设备,我的脚本将需要 1000 秒以上才能完成对所有设备的推送。
我想知道的是,我们可以将所有设备的post请求发送到google c2dm吗?如果可以的话怎么办?
我正在使用 PHP 脚本。
这是我将消息推送到设备的代码:
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;
}
如果我有更多设备,我会像这样循环它:
while($row = mysql_fetch_assoc($result)) {
sendMessageToPhone($authCode, $row['deviceRegistrationId'], GOOGLE_MSG_TYPE, $messageText, $infoType, $messageInfo);
}
感谢帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
身份验证是整个过程中最广泛(及时)的操作,这可能是每次发送之间有 1 秒延迟的原因。
为了加快该过程,您不应每次都进行身份验证。
只需验证一次,即可获取验证令牌。该令牌具有一定的 TTL,但 Google 未指定任何内容。
然后循环您的设备,并使用之前的身份验证令牌发送。身份验证令牌可以更改(很少),并且可以在响应标头
Update-Client-Auth
中找到。根据设备,所有过程不应花费更多的几百毫秒。
还可以考虑使用 stream 而不是curl
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