【问题标题】:Sending Android push notifications one shot instead of one by one一次性发送 Android 推送通知,而不是一一发送
【发布时间】:2015-05-10 20:44:47
【问题描述】:

我正在尝试向 GCM ID 存储在 MySQL 数据库中的用户发送推送通知。 问题在于我的代码,我正在获取 ID 并在 while 循环中一一发送。如果用户数量很大,即有很多 ID(大约 700),服务器会给出内部错误......而一旦我减少数量(大约 200),代码运行良好,我没有得到任何错误。

我的问题是,有没有办法一次性发送推送通知而不是一个接一个。

下面是我使用发送的代码。

包括(“/home/myHome/db/db.php”);

$resultTokenUser = $mysqli->query ('select * from android');

if ($resultTokenUser->num_rows > 0)
{               
    while ( $row = $resultTokenUser->fetch_assoc () )
    {           
        $token = $row ['gcm_regid'];
        $registatoin_ids = array ("$token");

        $messageToSend = array (
                "notificationMessage" => "Hello",
        );

        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array (
                'registration_ids' => $registatoin_ids,
                'data' => $messageToSend 
        );

        define ( "GOOGLE_API_KEY", "myKey" );
        $headers = array (
                'Authorization: key=' . GOOGLE_API_KEY,
                'Content-Type: application/json' 
        );
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( $fields ) );
        $result = curl_exec ( $ch );
    }
}

curl_close ( $ch );

【问题讨论】:

    标签: android push-notification google-cloud-messaging push


    【解决方案1】:

    GCM HTTP Connection Server - Source

    从您的数据库中检索注册 ID 作为数组,并用每个注册 ID 填充 $registatoin_ids 数组,然后只发送一次。

     $registatoin_ids = array();
        for($i=0;$i<$reg_id_count;$i++) {
    
            array_push($registatoin_ids, $row[$i]['gcm_regid']);
    
        }
    

    【讨论】:

      【解决方案2】:

      是的,您可以一次性向多达 1000 台设备发送通知。您可以一次创建一个包含 1000 个注册 id 的数组,并将其放入请求的 registration_ids 值中。

      $registration_ids=array("id1", "id2",…...." Id1000");
      
      $fields = array(
          'registration_ids' => $registatoin_ids,
          'data' => $message,
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        相关资源
        最近更新 更多