0

I have implemented the push notification in my Android App.

Now, I encounter the famous problem of canonicals id. If I uninstall the app without logout, so without delete the device_ids from my database, when I reinstall the app I receive notification that is not for the new user.

Google suggests to use the canonical ids for this problem, but I don't understand where I must intercept it and change id in my database. I have this PHP page that sends the notification:

    $gcm=new GCM();
    //get the array of all id associated to the user
    $row= (query to get registration_ids from my database);
    while($row = mysql_fetch_assoc($result)){
        array_push($registration_ids, $row['id_device']);
    }
    //create a message to send
    $mymessage="my message"; 
    $message=array("message"=>$mymessage);
    //send the notification and take the result
    $result_android=$gcm->send_notification($registration_ids,$message);
    echo $result_android;


   //class that send the notification
   class GCM{
   function __construct(){}
    public function send_notification($registatoin_ids,$message){
    // GOOGLE API KEY
    define("GOOGLE_API_KEY","xxxxxxxxxxxxxxxxx");
    $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;
}

Where should I to take the canonical_ids and insert it in my database? Server side or client side? I'm very confused.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
diegocom
  • 338
  • 1
  • 7
  • 22
  • Putting some comments in your code would help others to answer your question. Making the title more question like would also help. – cmbarbu Apr 03 '15 at 11:46
  • I'm sorry, I have edited my question – diegocom Apr 03 '15 at 11:56
  • Have a look at the answer of SO post, might be relavant to what you are looking for, Lot of information packaged here. http://stackoverflow.com/questions/27072824/how-to-get-canonical-id-from-gcm Hope that Helps!! – AniV Apr 03 '15 at 22:40
  • Thanks for the information. Then I must get the canonical ID in server side and overwrite the old reg id that I did the request in my database? But in this way the client side always receive a bad request at last once, it's right? – diegocom Apr 04 '15 at 10:52

2 Answers2

0

Now, when I send a push notification to a device with multiple registration id the result of the php page that send the request is this:

array(2) { ["registration_ids"]=> array(1) { [0]=> string(162) "XXXXXX(old reg id)XXXXXXX" } ["data"]=> array(1) { ["price"]=> string(24) "Hi, I’m a push notification!" } }
{"multicast_id":7004172909649400096,"success":1,"failure":0,"canonical_ids":1,"results":[{"registration_id":" XXXXXX(canonical reg id)XXXXXXX ","message_id":"0:1428420202799897%73660ba9f9fd7ecd"}]}

What should I do to not send a wrong notification?

diegocom
  • 338
  • 1
  • 7
  • 22
0

You need to read this working Example to solve your answer. Basically we have to update canonical id with existing duplicate registration id. In Response of GCM Notification one thing is similar i.e position of array that helps to update canonical id with existing registration id to avoid duplicate messages.

Community
  • 1
  • 1
Raghu
  • 31
  • 7