android使用google c2dm一次性向许多设备发送通知

Kan*_*ika 5 php android push-notification android-c2dm

我已经使用google c2dm成功实现了android推送通知.我总是发送一个设备的发布请求,一个设备延迟1-2秒.因此,如果我有1000个设备,我的脚本将需要超过1000秒才能完成对所有设备的推送.

我想知道的是,我们可以将所有设备的发布请求发送到谷歌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;
}
Run Code Online (Sandbox Code Playgroud)

如果我有更多设备,我会像这样循环:

while($row = mysql_fetch_assoc($result)) {

    sendMessageToPhone($authCode, $row['deviceRegistrationId'], GOOGLE_MSG_TYPE, $messageText, $infoType, $messageInfo);

}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

gru*_*unk 2

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

为了加快该过程,您不应每次都进行身份验证。只需验证一次,即可获取验证令牌。该令牌具有一定的 TTL,但 Google 未指定任何内容。然后循环您的设备,并使用之前的身份验证令牌发送。身份验证令牌可以更改(很少),并且可以在响应标头中找到Update-Client-Auth

根据设备,所有过程不应花费更多的几百毫秒。

还可以考虑使用而不是curl