我无法推送通知

时间:2016-04-09 14:46:42

标签: android laravel push-notification

我无法推送通知 我的回答是:

"{\"multicast_id\":8319582670472068768,\"success\":0,\"failure\":3,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidRegistration\"},{\"error\":\"InvalidRegistration\"},{\"error\":\"InvalidRegistration\"}]}"

代码:

public function sendNotification($registration_ids,$message){


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

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

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

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));

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

    error_log("Response from GCM Server:".$result);

    curl_close($curl);
    return $result;

1 个答案:

答案 0 :(得分:2)

如果您的API密钥无效,通常会发生此问题。响应是什么状态代码?您可以在此处阅读更多内容:https://developers.google.com/cloud-messaging/http#checkAPIkey

  

如果您收到401 HTTP状态代码,则您的API密钥无效。否则你   应该看到这样的事情:

     

{"multicast_id":6782339717028231855,"success":0,"failure":1, "canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

您可以更方便地使用为您处理GCM消息传递的Laravel程序包:https://github.com/davibennun/laravel-push-notification/

请告诉我您收到的状态代码。