PHP GCM 错误消息 MismatchSenderId

我面临 GCM 推送通知的问题.我收到以下错误.

I am facing the problem with GCM push notification. I am getting the following error.

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

以下是代码.任何帮助将非常感激.提前致谢.

Following is the code. Any help would be really appreciated. Thanks in Advance.

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;
}

推荐答案

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

"MismatchSenderId" is the obvious problem that we are getting nowadays.

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

Here are the possible cases that cause this problem.

案例 1:发件人 ID 不匹配 ->请检查您正在使用的项目编号.正确与否.

Case 1: Mismatching Sender ID -> Please check the Project number which you are using. If it's is correct or not.

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

Case 2: Wrong API Key -> Please be sure that you are using the same API_Key or not. And in most of the cases, we need to generate Server_Key instead of Android_Key.

案例 3:错误的设备 ID ->大多数情况下,问题是由于错误的设备 ID(GCM 生成的注册 ID)所致.

Case 3: Wrong Device's ID -> Most of the time the problem is due to the wrong Device ID(Registration ID generated by GCM).

请确保每当您生成新的 API 密钥时,您设备的设备 ID 都会更改.然后大概需要5五分钟才能见效.

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

Note : Your device id is bound with the API KEY.

所以....

--已创建新密钥.

--GCM for Android 已开启";在谷歌开发.控制台.

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

--设备注册到后端很好(Android 项目正在做它的工作).服务器上的设备密钥.

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

--发送到设备.失败!每次都从 GCM 返回相同的消息.

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

回顾一下.这不是 Android Studio、Android 操作系统或设备问题.GCM 服务器甚至不会尝试将消息发送到设备.我的服务器发送到 GCM,它返回消息...

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"}]} 

到服务器.据我所知,这意味着设备的 ID(在注册推送时返回到设备的 ID,以及保存在后端(在控制面板中)的 ID 不匹配,或者以某种方式与 API 无关发送消息时使用的密钥.

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.

当然,发送是从我的服务器开始,然后转到 GCM,然后转到设备.

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

这是没有发生的事情.消息从我的服务器发送到 GCM,然后返回到我的服务器 - 出现错误.

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

希望它能解决您的问题.

Hope it will solve your problem.

相关文章