Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: error "mismatch sender id" while pushing notification to android device though PHP and GCM

i'm trying to send a notification to my phone via my .php page... everything is set up correctly, but i get the error:

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

i don't know why because the sender id is right, the api also (i've tried the server key and the browser key, just to be sure).

i really don't know where i get wrong!

in my app i have only the sender id and all have gone right, in my server i've got the key for browser (now):

<?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;
}
like image 497
D Ferra Avatar asked Jan 15 '14 13:01

D Ferra


1 Answers

I had the same problem.

The solution is use my Project Number instead of API_KEY for the sender_id in the android app. On the server script you have to keep the API_KEY.

You can see your Project Number in the tab "Overview" inside your project on Google Developers Console.

like image 159
M_iserte Avatar answered Sep 27 '22 18:09

M_iserte