PHP GCM 错误信息 MismatchSenderId

8
我遇到了GCM推送通知的问题。我收到了以下错误信息。
{
  "multicast_id":4630467710672911593,
  "success":0,
  "failure":1,
  "canonical_ids":0,
  "results":[{
      "error":"MismatchSenderId"
  }]
}

以下是代码。非常感谢您的帮助。提前致谢。
public function gcmPush() 
{
    $regId = "APA91bHFcgOssQZEqtdUk3EC1ojwC5-LVG3NPV2bMqKyC9rPymR6StmAbz-N7Ss8fnvruZhWWNrR3lmBqpjQItlu00AKHPbltBclUJF-EfC5qG4CF2xiuYYC0NCf8u5rbiYFk8ARhIT4lY2AEPWzGpl1OtTvQEC0gA"; 
    $registatoin_ids = array($regId); 
    $message = array("msg" => 12345); 

    $this->send_notification($registatoin_ids, $message);
}

public function send_notification($registatoin_ids, $message) 
{
  // Set POST variables
  $url = 'https://android.googleapis.com/gcm/send';         
  define('GOOGLE_API_KEY', 'AIzaSyBavsIgQKo1Nf9wKZ5o_fGvE_6MI52LFR0');
  $fields = array(
    'registration_ids' => $registatoin_ids,
    'data' => $message,
  );
  $headers = array(
   'Authorization: key=' . GOOGLE_API_KEY,
   '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);
  echo $result;
}

可能是重复的问题:使用GCM发送消息时,我一直收到"MismatchSenderId"响应 - Pankaj Kumar
嗨,请查看这个链接:https://dev59.com/f2gu5IYBdhLWcg3wdG2o,希望对你有用。 - Raju Dudhrejiya
1个回答

9

"MismatchSenderId" 是我们现在遇到的明显问题。

以下是可能导致此问题的情况。

情况1:发送者ID不匹配 -> 请检查您正在使用的项目编号是否正确。

情况2:错误的API密钥 -> 请确保您是否使用相同的API密钥。在大多数情况下,我们需要生成Server_Key而不是Android_Key。

情况3:错误的设备ID -> 大多数时候,问题是由于错误的设备ID(由GCM生成的注册ID)引起的。

请确保每当您生成新的API密钥时,您的设备ID会更改。然后它将需要大约5分钟才能生效。

注意:您的设备ID与API KEY绑定。

所以....

--New Key created.

--GCM for Android Turned "on" in Google Dev. Console.

--Device registered with backend fine (Android Project is doing its job). Device key on the server.

--Send to device. Fail! The same message is returned from GCM everytime.

To Recap. This is NOT an Android Studio, Android OS, or Device issue. The GCM servers are not even trying to send the message to the device. My server sends to GCM, it returns the message...

{"multicast_id":6047824495557336291,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}

to the server. As far as I can tell this means the Device's ID (the one returned to the device when it registered for a push, and the one saved on the backend (in the control panel) does not match, or is somehow not associated with the API Key used when sending the message.

Sending, of course, starts on my server, goes to GCM, then goes to the device.

This is what's not happening. The message goes from my server to GCM and back to my server - with the error.

Super frustrating as all of you can imagine - we've all been through this nightmarish stuff before :-)

来源:https://www.buzztouch.com/forum/thread.php?tid=C3CED924C86828C2172E924


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接