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.