6

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;
}
Dexter
  • 4,036
  • 3
  • 47
  • 55
D Ferra
  • 1,223
  • 3
  • 12
  • 21
  • beacause of sender id of device is different then server sender id.you have to use same server's sender id as device sender id – Imtiyaz Khalani Jan 15 '14 at 13:40
  • ? on devices i put this: https://cloud.google.com/console/project/yyyyyyyyyyyyyyyy/apiui/api/googlecloudmessaging (only the yyyyyyyyyyyyyyyy). so this must be ok – D Ferra Jan 15 '14 at 13:43
  • user code.google.com/apis/console/b/0/?noredirect&pli=1#project:SENDER_KEY:access – Imtiyaz Khalani Jan 15 '14 at 13:48
  • ? what? i don't understand you. they told this: /** * Substitute you own sender ID here. This is the project number you got * from the API Console, as described in "Getting Started." */ String SENDER_ID = "Your-Sender-ID"; – D Ferra Jan 15 '14 at 13:51
  • you got your sender id where i have write SEDNER_KEY in your google project Url – Imtiyaz Khalani Jan 15 '14 at 14:13
  • yes, and i've got the right sender id (your url create an infinite redirect). i'm sure that the two values is right bacause it's not the first time i use gcm... the problem must be in another point – D Ferra Jan 15 '14 at 14:17
  • Is it possible you recently changed your project ID? Perhaps you are using an old Registration ID that was returned by GCM for a different project ID than the one you are currently using. – Eran Jan 15 '14 at 15:20
  • If you are sure about the senderid is correct, clean and reinstall the project. This had worked for me. I'd followed http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/ for the task. – Dexter Dec 24 '14 at 07:30
  • good answer can be found here: http://stackoverflow.com/questions/16683874/new-senderid-does-not-work-on-gcm-mismatchedsenderid – Oded Regev Dec 28 '14 at 13:24

4 Answers4

4

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.

M_iserte
  • 98
  • 5
0

Please run below script in your terminal

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 because within same device you have logged with different keys. to solve this problem uninstall an app and run it again and update the registration key. and then run the CURL script in your terminal which I post above it will give success message and you will get a notification to your device

nKandel
  • 2,543
  • 1
  • 29
  • 47
Raj Kumar
  • 688
  • 8
  • 18
0

I had the same issue. the exact issue was I forgot to change the new google-services.json which I have downloaded after importing my project to Firebase from GCM.

Make sure you update the google-services.json after importing

Akhil Dad
  • 1,804
  • 22
  • 35
0

this error just come because of your firebase server key and google-service.json file not same

  • First check your project on firebase and get download google-service.json file.
  • then put into your android project
  • then retrieve server key from cloud messaging tab from firebase and set in your php file
Jatin Bansal
  • 123
  • 8