如果超过一定数量的用户,PHP通知功能会错误

时间:2016-07-01 05:50:39

标签: php android api push-notification

以下代码适用于2个或3个用户,只要我将查询限制为3个人即可。当我为2000人尝试它时,它不再发送通知。我还收到成功:0 作为回应。

public function send_notification($registatoin_ids, $message) {

            // Set POST variables

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



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


            $headers = array(
                'Authorization: key=' . $googleapikey,
                'Content-Type: application/json'
            );
            // 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);

            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));




            // Execute post
            $result = curl_exec($ch);

            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            // Close connection
            curl_close($ch);

            $result1=json_decode($result);



    }

我也使用上面的函数来调用下面的函数来发送我的通知。 我的问题是,只有2或3个用户才有效,但当用户超过2000时,它无法按预期工作(发送通知)。

 $qry = mysql_query("SELECT deviceToken FROM app_devices");

            $devices = array();
            while($row = mysql_fetch_assoc($qry))
            {

                $devices[] =  $row['deviceToken'];

            }
             $gcm->send_notification($devices, $message);

2 个答案:

答案 0 :(得分:2)

据我所知,我们一次最多可以从GCM发送1000条通知,所以如果你想发送更多的通知,请将用户数除以1000组并以这种方式发送请求。

对于Eg: 如果您想发送2000个通知,则进行2次呼叫,每次呼叫都有1000个用户。

答案 1 :(得分:0)

我建议您使用Firebase Cloud Messaging,因为它是Google Cloud Messaging.的演变 试着用这个:

    $message = array ("message" => $bodyMail, "to" => $to, "From" => $from);
$id = array($to);
$message_status = sendPushNotification( $message, $id );

echo $message_status;

// Send push notification via Firebase Cloud Messaging


function sendPushNotification( $data, $ids )
{
    // Insert real FCM API key from the Google APIs Console

    $apiKey = "YourApiKey";
    // Set POST request body
    $fields = array(
        'registration_ids'  => $ids,
        'data'              => $data
    );

    // Set CURL request headers
    $headers = array(
        'Authorization: key =' . $apiKey,
        'Content-Type: application/json'
    );

    // Initialize curl handle
    $ch = curl_init();

    // Set URL to GCM push endpoint
    curl_setopt( $ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );

    // Set request method to POST
    curl_setopt( $ch, CURLOPT_POST, true );

    // Set custom request headers
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

    // Get the response back as string instead of printing it
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    // Set JSON post data
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

    // Actually send the request
    $result = curl_exec( $ch );

    // Handle errors
    if ( curl_errno( $ch ) )
    {
        echo 'FCM error: ' . curl_error( $ch );
    }

    // Close curl handle
    curl_close( $ch );

    // Debug GCM response
    return $result;
}