使用firebase服务器端获取MismatchSenderId

时间:2017-11-22 09:37:43

标签: php firebase firebase-cloud-messaging phonegap

我正在尝试使用存储在我的服务器中的php脚本发送通知,而我正在获取MismatchSenderId。

$to="device_id";
$title="MYAPP Push";
$message=" MYAPP Push Notification Message";

sendPush($to,$title,$message);

function sendPush($to,$title,$message){
  // API access key from Google API's Console
  // replace API
  define( "API_ACCESS_KEY", "server_key_provided_by_firebase");

  $registrationIds = array($to);

  $msg = array(
    'message' => $message,
    'title' => $title,
    'vibrate' => 1,
    'sound' => 1
    // you can also add images, additionalData
  );

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

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

  $ch = curl_init();
  curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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 );
  echo $result;
}

这是我运行php脚本时遇到的错误: {"multicast_id":7804476702639319453,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}

我已经在stackoverflow中检查了每个问题并且无法解决它。

我正在使用firebase(火花计划)并使用Phonegap开发应用程序。我认为这与应用程序无关

任何想法???

2 个答案:

答案 0 :(得分:1)

将google-service.json文件复制到根文件夹(包含www,config.xml等)。

第1步: 登录您的firebase控制台。

enter image description here

第2步: 在项目概述设置上,复制 Cloud Messaging ServerKey

我的关键点:

`AAAAjXzVMKY:APA91bED4d53RX.....bla bla

第3步: 更换钥匙

define( "API_ACCESS_KEY", "My key");

最后测试应用程序:D

我成功发送了使用节点的推送通知。



var gcm = require('node-gcm');

// Replace these with your own values.
var apiKey = "MY_SERVER_KEY";
var deviceID = "MY_DEVICE_ID";

var service = new gcm.Sender(apiKey);
var message = new gcm.Message();
message.addData('title', 'Hi');
message.addData('body', 'BLA BLA BLA BLA');
message.addData('actions', [
  { "icon": "accept", "title": "Accept", "callback": "app.accept"},
  { "icon": "reject", "title": "Reject", "callback": "app.reject"},
]);

service.send(message, { registrationTokens: [ deviceID ] }, function (err, response) {
	if(err) console.error(err);
	else 	console.log(response);
});




答案 1 :(得分:0)

documentation for this error州:

  

注册令牌与某组发件人绑定。当一个   客户端应用程序注册FCM,它必须指定哪些发件人   允许发送消息。您应该使用其中一个发件人ID   将消息发送到客户端应用程序。如果你切换到另一个   发件人,现有的注册令牌将无效。

您应该仔细检查您是否使用了用于生成应用程序构建的google-services.json文件的同一项目中的服务器密钥。 project_number文件顶部的google-services.json必须与Firebase控制台的项目设置云消息传递选项卡中显示的发件人ID相同。