【问题标题】:Android GCM packet sendingAndroid GCM 数据包发送
【发布时间】:2013-06-17 08:40:31
【问题描述】:

我可以使用Android GCM在一个请求中发送多个推送通知吗?

【问题讨论】:

  • 是的。你可以。而是一件一件。不在群里。可以循环发送。

标签: php android google-cloud-messaging


【解决方案1】:

是的,你可以。我使用此代码以 1000 个用户为一组发送它们:

$url = 'https://android.googleapis.com/gcm/send';
$fields['data'] = $message;

$headers = array(
        'Authorization: key=' . $api_key,
        'Content-Type: application/json'
);
//echo "Reg ids: ".var_dump($fields['registration_ids'])."<br>";
    for ($i = 0; $i < 9998; $i++) {
        $registration_ids[$i+2] = $i;
    }
echo "Total necessary requests to GCM: ".(intval(count($registration_ids)/1000)+1)."<br>";
for ($j = 0; $j < count($registration_ids)/1000; $j++) {
    echo "Processing request number ".($j + 1)."...<br>";
    $fields['registration_ids'] = array();
    for($i=0;$i < 1000 && ($i+1000*$j) < count($registration_ids);$i++){
        $fields['registration_ids'][$i] = $registration_ids[$i+1000*$j];
    }
    echo "Sending push to devices: ".(1000*$j)." - ".(count($fields['registration_ids'])+1000*$j)."<br>";

    // Open connection
    $ch = curl_init();
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 //var_dump($fields);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post

    $result = curl_exec($ch);
    //$result = true;
    if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
    }
    else
        echo "Curl success<br>";

    echo $result."<br>";

我希望你觉得这段代码有用

【讨论】:

  • 谢谢,但我的意思是通过一个请求发送。因此,根据 docs 和 @Chintan Rathod 的说法,每个请求只能为不同的设备发送相同的通知。
  • 你不能说它是不同的设备。我认为你有推送通知的含义,比如点对点通知。但 GCM 不是这个意思。如果您从服务器触发任何推送通知,它将尽可能多地将其传递给在 GCM 中注册的设备。
  • 只是为了澄清。我注册了 2 个不同的设备,我想向第一个设备发送通知“Hello”,向第二个设备发送“Hi”。一个请求。有可能吗?
  • @DmitryTeplyakov,回答:。您必须在从服务器一一发送时分叉您的消息。就像您需要在数据库中设置一些标志,这些标志将从数据库中获取该“值”并基于它发送消息。
  • 好的,我明白了,有两种可能的答案:您可以通过一个请求向多达 1000 台设备发送相同的消息,但您不能向同一设备发送两条不同的消息例如,这是由于他们正在检测的 json 对象的性质。它不能包含内部的 json 对象数组或类似的东西。 要发送两个不同的推送,您需要 curl_exec() 两次。
【解决方案2】:

我们最多可以向 1000 台设备发送相同的消息。 https://developers.google.com/cloud-messaging/http-server-ref#table1

但是我们不能通过一个请求向不同的设备发送不同的通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多