gpt4 book ai didi

php - android使用谷歌c2dm一次向许多设备推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:36 26 4
gpt4 key购买 nike

我已经使用 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);

}

感谢您的帮助。

最佳答案

身份验证是所有过程中最广泛(及时)的操作,这可能是每次发送之间有 1 秒延迟的原因。

为了加快这个过程,你不应该每次都进行身份验证。只需授权一次,并获得授权 token 。此 token 具有特定的 TTL,但 Google 未指定任何内容。然后遍历您的设备,并使用以前的身份验证 token 发送。身份验证 token 可以更改(很少)并且可以在响应 header Update-Client-Auth 中找到。

设备的所有过程不应超过几百毫秒。

也可以考虑使用 stream而不是 curl

关于php - android使用谷歌c2dm一次向许多设备推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8698704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com