当授权密钥是我的服务器密钥时,Firebase MismatchSenderID [英] Firebase MismatchSenderID when Authorization key is my Server key

查看:542
本文介绍了当授权密钥是我的服务器密钥时,Firebase MismatchSenderID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我得到 MismatchSenderId 作为错误,我的服务器密钥作为我的授权密钥(服务器密钥)。
而另一方面,如果我的设备令牌作为我的授权密钥,但是我的设备上没有收到通知,我将从push_notification()返回结果。



我确定我的令牌是正确的,因为我可以使用firebase(单一设备)控制台中的令牌发送给单个设备。



然后导致我到这个另一个问题,但是我想确认这个问题,然后移动到通知部分

我真的很迷路,因为所有的 MismatchSenderId 错误都是用正确的服务器密钥替换的。

 <?php 
需要dbconfig.php;

$ sql =SELECT token FROM tokendb;
$ result = mysql_query($ sql)or die($ sql。< br />< br />。mysql_error());
$ tokens = array();

while($ row = mysql_fetch_assoc($ result))
{
echo $ row ['token']; //与logcat(visual studio)
$ tokens [] = $ row [token]中的令牌显示相比,显示的是相同的标记。

echo'< form id =form1name =form1method =postaction =>。
'< p>从< br>'开始。
'< input type =textsize =100maxlength =100name =Fromid =From/>< br>'
'< p>标题< br>'。
'< input type =textsize =100maxlength =100name =Titleid =Title/>'。
'< br>'。
'< input type =submitname =Sendid =Sendvalue =Send/>'。
'< / form>';



$ message = array(message=>通知测试);
$ message_status = sendFCMMessage($ message,$ tokens);
echo $ message_status;

函数sendFCMMessage($ message,$ target){

// FCM API端点
$ url ='https://fcm.googleapis.com / FCM /发送';

$ fields = array();
$ fields ['body'] = $ message;
if(is_array($ target)){
$ fields ['registration_ids'] = $ target;
} else {
$ fields ['to'] = $ target;

$ b $ // header_content_type api key
$ headers = array(
'Content-Type:application / json',
'Authorization: key = ********'
);
// CURL请求将通知路由到FCM连接服务器(由Google提供)
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ fields));
$ result = curl_exec($ ch);
if($ result === FALSE){
die('Oops!FCM Send Error:'。curl_error($ ch));
}
curl_close($ ch);
返回$ result;
}

?>

编辑:这个简短的php代码也给我 MismatchSenderId error

 <?php 
$ ch = curl_init ://fcm.googleapis.com/fcm/send);
$ header = array('Content-Type:application / json',
'Authorization:key = ***(serverkey from project));
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ header);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,{\notification \:{\title \:\Test desde curl \,\text \:\ Otra prueba \},\to \:\我的设备的令牌\});

curl_exec($ ch);
curl_close($ ch);
?>

UPDATE



卸载并重新安装应用程序可解决问题。 MismatchSenderId 当您尝试发送到与另一个发件人(项目)关联的注册令牌时,会遇到错误。从文档:


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

确保服务器密钥您使用的是与注册令牌关联的相同发件人项目。您可以通过检查您的google-services.json文件来查看是否存在与您用来发送消息的Firebase项目中显示的发件人ID 相匹配的发件人ID

对于未收到消息的问题,您的代码发送的有效负载的结构有点不清楚。尝试使用通知和/或数据消息有效内容检查它是否是正确结构的消息有效内容。


Really weird and no one has ever asked this.

I'm getting MismatchSenderIdas an error with my server key as my authorization key(server key). And on the other hand, I'm getting success as a return result from my push_notification() if my device's token as my authorization key, but no notification is received on my device.

I'm sure that my token is correct because I'm able to send to a single device with token in firebase (single device) console.

Which then leads me to this another question, but then I would like to confirm this issue before moving to the notification part

I'm pretty lost actually, since that all MismatchSenderId errors were resolved with replacing with the correct server key.

    <?php 
    require "dbconfig.php";

    $sql = "SELECT token FROM tokendb";
    $result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
    $tokens = array();

    while($row = mysql_fetch_assoc($result))
    {
        echo $row['token']; //this shows the same token compared to the token show in logcat(visual studio)
        $tokens[] = $row["token"];

      echo '<form id="form1" name="form1" method="post" action="">'.
    '<p>From<br>'.
    '<input type="text" size="100" maxlength="100" name="From" id="From" /><br>'.
    '<p>Title<br>'.
    '<input type="text" size="100" maxlength="100" name="Title" id="Title" />'.
    '<br>'.
    '<input type="submit" name="Send" id="Send" value="Send" />'.
    '</form>';

    }

    $message = array("message" => "notification test");
    $message_status = sendFCMMessage($message, $tokens);
    echo $message_status;

    function sendFCMMessage($message,$target){

       //FCM API end-point
       $url = 'https://fcm.googleapis.com/fcm/send';

       $fields = array();
       $fields['body'] = $message;
       if(is_array($target)){
        $fields['registration_ids'] = $target;
       }else{
        $fields['to'] = $target;
       }

       //header with content_type api key
       $headers = array(
        'Content-Type:application/json',
            'Authorization:key=********'
       );
       //CURL request to route notification to FCM connection server (provided by Google)           
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);
       if ($result === FALSE) {
        die('Oops! FCM Send Error: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
    }

    ?>

EDIT: This short php code is also giving me MismatchSenderId error

  <?php
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
$header=array('Content-Type: application/json',
"Authorization: key=***(serverkey from project)");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"notification\": {    \"title\": \"Test desde curl\",    \"text\": \"Otra prueba\"  },    \"to\" : \"my device's token\"}");

curl_exec($ch);
curl_close($ch);
?>

UPDATE

Uninstalling and reinstalling the app resolved the issue.

解决方案

The MismatchSenderId error is encountered when you are attempting to send to a registration token that is associated with a different Sender (Project). From the docs:

A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app. If you switch to a different sender, the existing registration tokens won't work.

Make sure that the Server Key you are using is from the same Sender Project that the registration token is associated to. You can do this by checking your google-services.json file and see if the Sender ID there matches the Sender ID visible in the Firebase Project you are using to send the message.

For the matter of not receiving messages, It's a bit unclear as to what is the structure of payload that your code is sending. Try checking if it is a properly structured message payload with a notification and/or data message payload.

这篇关于当授权密钥是我的服务器密钥时,Firebase MismatchSenderID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆