2017-04-06 221 views
1

真的很奇怪,也没有人问过这个问题。当授权密钥是我的服务器密钥时Firebase MismatchSenderID

我得到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); 
?> 

UPDATE

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

+0

哇chigga是天才 –

回答

1

当您尝试发送到与其他发件人(项目)关联的注册令牌时,会遇到MismatchSenderId错误。从文档:

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

确保您使用的是服务器密钥是从该登记令牌关联同一发件人项目。您可以通过检查您的google-services.json文件并查看发件人ID是否与您用于发送消息的Firebase项目中可见的发件人ID匹配。

对于未收到消息的问题,您的代码发送的有效负载结构有点不清楚。尝试检查它是否是一个正确结构的消息有效载荷,具有notification和/或data消息有效载荷。

+0

嗨,是的,我已确保服务器密钥来自我正在处理的同一个项目。由我的项目下的Firebase提供的google-services.json也包含相同的发件人ID – Lozy

+0

奇数。您是否尝试卸载并重新安装应用程序以获取新的注册令牌,然后尝试再次发送消息? –

+0

我曾尝试卸载应用程序并重新安装应用程序昨天,但不知何故今天解决。谢谢 – Lozy

0

让您的服务器密钥从正确的位置开始 现在,从Firebase获取通知的服务器密钥有点棘手。

具体操作步骤如下:

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

,并采取你的服务器密钥,并从他们的发送者ID,其将工作为你。

卸载该应用程序并重新安装并尝试。

+0

让我知道如果你仍然得到problem.thanks – 9spl