Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向多个设备发送Android推送通知的PHP代码_Php_Android_Google Cloud Messaging_Android Notifications - Fatal编程技术网

向多个设备发送Android推送通知的PHP代码

向多个设备发送Android推送通知的PHP代码,php,android,google-cloud-messaging,android-notifications,Php,Android,Google Cloud Messaging,Android Notifications,下面是php代码,我在其中对GCM服务器进行了curl调用: //CODE TO SEND PUSH NOTIFICATION define('API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXXXXX'); $i = 0; $result1 = mysql_query("SELECT * FROM `my_devices`") o

下面是php代码,我在其中对GCM服务器进行了curl调用:

//CODE TO SEND PUSH NOTIFICATION 
                        define('API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXXXXX');
                        $i = 0;
                        $result1 = mysql_query("SELECT * FROM `my_devices`") or die($log->lwrite("Error" . mysql_error()));
                        while ($row2 = mysql_fetch_array($result1)) {
                            $i = $i + 1;
                            $msg = array(
                                'message' => $resultTextValue
                            );

                            $fields = array(
                                'registration_ids' => array($row2['gcmId']),
                                'data' => $msg
                            );

                            $headers = array(
                                'Authorization: key=' . API_ACCESS_KEY,
                                'Content-Type: application/json'
                            );

                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
                            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);
                        }
我的代码正在开发中,最多注册了10个设备。因此,需要相当长的时间才能将通知发送到所有设备

我担心的是:部署应用程序时,我将有数千台设备发送推送通知推送通知呼叫不会花费大量时间吗?


我是PHP新手,从一些示例应用程序中复制了上述代码。

首先将所有注册ID获取到一个数组中

$regIDS = array(); // set variable as array

  // get all ids in while loop and insert it into $regIDS array
 while ($row2 = mysql_fetch_array($result1)) {
array_push($regIDS ,$row2['gcmId'])
}
然后在$fields中,提及数组变量名$regIDS,而不是数组($row2['gcmId']

$fields = array(
        'registration_ids' => $regIDS ,
        'data' => $msg
 );
现在,您可以在一条推送通知消息中向多个设备发送消息

示例图片=

这里我在数据库中只有两个regid,所以这里Success=2只在一次push通知调用中


如果要将同一消息发送到某一组设备,可以使用“注册ID”(所有要发送消息的ID)和一条消息,这样你可以在一次呼叫中发送多条消息。否则你可能必须为每个收件人逐个发送消息。@Yazan你能提供一些代码吗?我是PHP新手,请检查此问题和所选答案如果每个令牌的消息不同怎么办?