GCM发件人ID不匹配 [英] GCM Sender Id Mismatch

查看:212
本文介绍了GCM发件人ID不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装的API密钥第三方GCM服务器,安装Android应用程序与 APP_SERVER_URL =HTTP://(localIPAddress):4430 / gcm.php shareRegId = 1,放在我的电脑的公网IP​​地址,而不是localIPAdderss在谷歌应用程序控制台允许的IP地址。

的Andr​​oid应用程序指出其Registration_id与第三方服务器上的共享,但测试,以从我的服务器发送邮件到应用程序时,它指出:

<$p$p><$c$c>{\"multicast_id\":7181230864355412704,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"MismatchSenderId\"}]}

我怎样才能得到这个工作?

gcm.php 在端口4430 Apache的htdocs目录:

 &LT; PHP
    //通用PHP函数发送GCM推送通知
   功能sendPushNotificationToGCM($ registatoin_ids,$消息){
        //谷歌云通讯GCM-API网址
        $ URL ='https://android.googleapis.com/gcm/send';
        $栏=阵列(
            registration_ids'=&GT; $ registatoin_ids,
            '数据'=&GT; $消息,
        );
        //谷歌云通讯GCM API密钥
        定义(GOOGLE_API_KEY,(我的控制台API密钥)); //我的API密钥形式谷歌控制台
        $头=阵列(
            授权:键='。 GOOGLE_API_KEY,
            内容类型:应用程序/ JSON
        );
        $ CH = curl_init();
        curl_setopt($ CH,CURLOPT_URL,$网址);
        curl_setopt($ CH,CURLOPT_POST,真正的);
        curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
        curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
        curl_setopt($ CH,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));
        $结果= curl_exec($ CH);
        如果($结果=== FALSE){
            死('卷曲失败:'curl_error($ CH));
        }
        curl_close($ CH);
        返回$结果;
    }
?&GT;
&LT; PHP    //这个块是张贴消息GCM上点击
    $ pushStatus =;
    如果(!空($ _ GET [推])){
        $ gcmRegID =的file_get_contents(GCMRegId.txt);
        $ pushMessage = $ _ POST [消息];
        如果(使用isset($ gcmRegID)及&安培;使用isset($ pushMessage)){
            $ gcmRegIds =阵列($ gcmRegID);
            $消息=阵列(M=&GT; $ pushMessage);
            $ pushStatus = sendPushNotificationToGCM($ gcmRegIds,$消息);
        }
    }    //这个块是从外部(移动应用)收到GCM REGID
    如果(!空($ _ GET [shareRegId])){
        $ gcmRegID = $ _ POST [REGID];
        的file_put_contents(GCMRegId.txt,$ gcmRegID);
        回声OK!;
        出口;
    }
?&GT;
&LT; HTML和GT;
    &LT; HEAD&GT;
        &LT;标题&gt;在PHP与谷歌LT云消息传递(GCM)服务器; /标题&GT;
    &LT; /头&GT;
    &LT;身体GT;
        &LT; H1&gt;在PHP与谷歌LT云消息传递(GCM)服务器; / H1&GT;
        &LT;形式方法=邮报行动=?gcm.php /推= 1&GT;
            &LT; D​​IV&GT;
                &LT; textarea的行数=2NAME =消息COLS =23占位符=消息通过GCM传送&GT;&LT; / textarea的&GT;
            &LT; / DIV&GT;
            &LT; D​​IV&GT;&LT;输入类型=提交值= /&GT通过GCM发送推送通知;&LT; / DIV&GT;
        &LT; /表及GT;
        &LT; P&GT;&LT; H3&GT;&LT; PHP的echo $ pushStatus; ?&GT;&下; / H3&GT;&下; / P&GT;
    &LT; /身体GT;
&LT; / HTML&GT;

教程来源:的http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/


解决方案

看起来你叫发件人ID gcm.register(senderID)与不是的发件人ID您的服务器。你必须加倍您的Andr​​oid code检查。它是由谷歌开发者控制台项目的项目编号。

错误进行全面介绍这里

I have setup the third party GCM server with the API key, setup the Android app with the APP_SERVER_URL= "http://(localIPAddress):4430/gcm.php?shareRegId=1" and placed my computers public IP address, not the localIPAdderss in the "Allowed IPs" in the Google App Console.

The Android app notes its' Registration_id is shared with the 3rd party server, yet when testing to send a message from my server to the app it notes:

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

How can I get this to work?

gcm.php in Apache htdocs on port 4430:

<?php
    //generic php function to send GCM push notification
   function sendPushNotificationToGCM($registatoin_ids, $message) {
        //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
        // Google Cloud Messaging GCM API Key
        define("GOOGLE_API_KEY", "(my console API key)"); // My API Key form Google console
        $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_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('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }
?>
<?php

    //this block is to post message to GCM on-click
    $pushStatus = "";   
    if(!empty($_GET["push"])) { 
        $gcmRegID  = file_get_contents("GCMRegId.txt");
        $pushMessage = $_POST["message"];   
        if (isset($gcmRegID) && isset($pushMessage)) {      
            $gcmRegIds = array($gcmRegID);
            $message = array("m" => $pushMessage);  
            $pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
        }       
    }

    //this block is to receive the GCM regId from external (mobile apps)
    if(!empty($_GET["shareRegId"])) {
        $gcmRegID  = $_POST["regId"]; 
        file_put_contents("GCMRegId.txt",$gcmRegID);
        echo "Ok!";
        exit;
    }   
?>
<html>
    <head>
        <title>Google Cloud Messaging (GCM) Server in PHP</title>
    </head>
    <body>
        <h1>Google Cloud Messaging (GCM) Server in PHP</h1> 
        <form method="post" action="gcm.php/?push=1">                                                
            <div>                                
                <textarea rows="2" name="message" cols="23" placeholder="Message to transmit via GCM"></textarea>
            </div>
            <div><input type="submit"  value="Send Push Notification via GCM" /></div>
        </form>
        <p><h3><?php echo $pushStatus; ?></h3></p>        
    </body>
</html>

Tutorial Source: http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/

解决方案

Looks like the sender ID you called gcm.register(senderID) with is not the sender ID of your server. You'll have to double check this in your Android code. It is the project number of your project from the Google Developers Console.

Full description of the error is here.

这篇关于GCM发件人ID不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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