Android:通过PHP和GCM向Android设备推送通知时出现“mismatch sender id”错误

6

我想通过我的.php页面向手机发送通知......一切都设置正确,但是我收到了错误:

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

我不知道为什么,因为发送者ID是正确的,API也是(我已经尝试了服务器密钥和浏览器密钥,只是为了确认)。
我真的不知道我哪里错了!
在我的应用程序中,我只有发送者ID,一切都进行得很顺利,在我的服务器上,我现在有浏览器密钥:
<?php require_once("../pi_classes/commonSetting.php");
include('../pi_classes/User.php');
ini_set("display_errors",1);
class GCM{
    function __construct(){}
    public function send_notification($registatoin_ids,$message){
        // GOOGLE API KEY
        define("GOOGLE_API_KEY","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        $url="https://android.googleapis.com/gcm/send";
        $fields=array(
            "registration_ids"=>$registatoin_ids,
            "data"=>$message,
        );
        var_dump($fields);
        $headers=array(
            "Authorization: key=".GOOGLE_API_KEY,
            "Content-Type: application/json"
        );
        $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_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
        $result=curl_exec($ch);
        if($result===FALSE){
            die("Curl failed: ".curl_error($ch));
        }
        curl_close($ch);
        echo $result;
    }
}
// ======================
//=INVIA LE NOTIFICHE AGLI UTENTI =
// ======================
$messaggio="Ciao, sono una notifica!";
$pushCounter=0;
$registatoin_ids=array();
$result=mysql_query("SELECT android_regi_id FROM user_details");
while($row=mysql_fetch_array($result)){
    $token=$row["android_regi_id"];
    if($token!=""){
        $registatoin_ids[]=$token;
        $pushCounter++;
    }
}
if($pushCounter>0){
    $gcm=new GCM();
    $message=array("price"=>$messaggio);
    $result_android=$gcm->send_notification($registatoin_ids,$message);
    echo $result_android;
}

由于设备的发送者ID与服务器的发送者ID不同,您必须使用相同的服务器发送者ID作为设备发送者ID。 - Imtiyaz Khalani
在设备上,我放置了以下内容:https://cloud.google.com/console/project/yyyyyyyyyyyyyyyy/apiui/api/googlecloudmessaging(只需替换 yyyyyyyyyyyyyyyy 部分)。 这样就可以了。 - D Ferra
用户 code.google.com/apis/console/b/0/?noredirect&pli=1#project:SENDER_KEY:access。 - Imtiyaz Khalani
什么?我不明白你的意思。他们说了这个:/** * 在这里替换您自己的发送者ID。这是您从API控制台获取的项目编号,如“入门”中所述。 */ String SENDER_ID = "您的发送者ID"; - D Ferra
你已经获得了发送者ID,在你的Google项目URL中,我需要在其中写SENDER_KEY。 - Imtiyaz Khalani
显示剩余4条评论
4个回答

4

我遇到了同样的问题。

解决方案是在Android应用程序中使用我的项目编号而不是API_KEY作为sender_id。在服务器脚本中,您必须保留API_KEY。

您可以在Google开发者控制台上的“概述”选项卡中查看您的项目编号。


0
请在您的终端中运行以下脚本:
curl -X POST \  
-H "Authorization: key=  write here api_key" \  
-H "Content-Type: application/json" \  
-d '{      
"registration_ids": [  
"write here reg_id generated by gcm"  
],  
"data": { 
"message": "Manual push notification from Rajkumar"
},
"priority": "high"
}' \
https://android.googleapis.com/gcm/send

MismatchSenderId 是因为在同一设备上使用了不同的密钥进行登录。要解决此问题,请卸载应用程序并重新运行它,然后更新注册密钥。然后在终端中运行我上面发布的CURL脚本,它将给出成功消息,并且您将收到通知到您的设备。


0

我也遇到了相同的问题。具体是我忘记更新新的google-services.json文件,这个文件是在我从GCM导入项目到Firebase之后下载的。

确保你在导入后更新google-services.json文件


0
这个错误是由于你的Firebase服务器密钥和google-service.json文件不一致导致的。
首先检查你的Firebase项目并下载google-service.json文件。
然后将其放入你的Android项目中。
接着从Firebase的云消息传递选项卡中检索服务器密钥,并在你的PHP文件中设置。
注意:本文翻译仅供参考,具体操作请以原文为准。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接