【问题标题】:Android: error "mismatch sender id" while pushing notification to android device though PHP and GCMAndroid:通过 PHP 和 GCM 向 Android 设备推送通知时出现错误“发件人 ID 不匹配”
【发布时间】:2014-01-15 13:36:50
【问题描述】:

我正在尝试通过我的 .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
  • ?在设备上我放了这个:cloud.google.com/console/project/yyyyyyyyyyyyyyyy/apiui/api/…(只有 yyyyyyyyyyyyyyyy)。所以这一定没问题
  • 用户代码.google.com/apis/console/b/0/?noredirect&pli=1#project:SENDER_KEY:access
  • ?什么?我不明白你。他们告诉这个:/** * 在此处替换您自己的发件人 ID。这是您从 API 控制台获得的项目编号 *,如“入门”中所述。 */ String SENDER_ID = "你的发件人 ID";
  • 你得到了你的发件人ID,我在你的谷歌项目Url中写了SEDNER_KEY

标签: android push google-cloud-messaging mismatch sender-id


【解决方案1】:

我遇到了同样的问题。

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

您可以在 Google Developers Console 的项目内的“概览”选项卡中查看您的项目编号。

【讨论】:

    【解决方案2】:

    请在终端中运行以下脚本

    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 脚本,它将给出成功消息,您将收到通知到您的设备

    【讨论】:

      【解决方案3】:

      我有同样的问题。确切的问题是我忘记更改从 GCM 将项目导入 Firebase 后下载的新 google-services.json

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

      【讨论】:

        【解决方案4】:

        这个错误只是因为你的 firebase 服务器密钥和 google-service.json 文件不一样

        • 首先在 firebase 上检查您的项目并下载 google-service.json 文件。
        • 然后放入你的android项目
        • 然后从 firebase 的云消息选项卡中检索服务器密钥,然后 在你的 php 文件中设置

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-08
          • 2014-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多