【问题标题】:Firebase MismatchSenderID when Authorization key is my Server key当授权密钥是我的服务器密钥时的 Firebase MismatchSenderID
【发布时间】:2017-08-31 21:03:23
【问题描述】:

真的很奇怪,从来没有人问过这个问题。

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

我确定我的令牌是正确的,因为我能够在 firebase(单个设备)控制台中使用令牌发送到单个设备。

这又将我引向另一个question,但我想在转到通知部分之前确认这个问题

实际上我很迷茫,因为所有MismatchSenderId 错误都已通过替换正确的服务器密钥得到解决。

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

    ?>

编辑:这个简短的 php 代码也给了我MismatchSenderId 错误

  <?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);
?>

更新

卸载并重新安装应用程序解决了这个问题。

【问题讨论】:

  • 哇 chigga 真是天才

标签: php android firebase firebase-cloud-messaging


【解决方案1】:

让您从正确的地方获取服务器密钥 现在从 firebase 获取通知的服务器密钥有点棘手。

以下是步骤:

转到控制台 -> 您的项目 -> 项目设置 -> 云消息(第二个选项卡)

并从他们那里获取您的服务器密钥和发件人 ID,这对您有用。

卸载应用重新安装试试看。

【讨论】:

    【解决方案2】:

    当您尝试发送到与不同发件人(项目)关联的注册令牌时遇到MismatchSenderId 错误。来自文档:

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

    确保您使用的服务器密钥来自与注册令牌关联的同一个发件人项目。您可以通过检查您的 google-services.json 文件并查看其中的 Sender ID 是否与您用于发送消息的 Firebase 项目中可见的 Sender ID 匹配来执行此操作.

    关于不接收消息的问题,您的代码发送的有效负载的结构有点不清楚。尝试使用notification 和/或data 消息负载检查它是否是结构正确的消息负载。

    【讨论】:

    • 嗨,是的,我确保服务器密钥来自我正在处理的同一个项目。我的项目下Firebase提供的google-services.json也包含相同的发件人ID
    • 奇数。您是否尝试过卸载并重新安装该应用以获得新的注册令牌,然后再次尝试发送消息?
    • 我昨天尝试卸载应用程序并重新安装应用程序,但不知何故今天解决了。谢谢
    猜你喜欢
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2013-07-16
    • 2022-06-14
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    相关资源
    最近更新 更多