注销后重新安装应用程序时的GCM和规范ID


GCM and canonical Ids when reinstalling the app witouth logout

我已经在我的Android应用程序中实现了推送通知。

现在,我遇到了著名的经典id问题。如果我在不注销的情况下卸载应用程序,也就是说,在不从数据库中删除device_ids的情况下,当我重新安装应用程序时,我会收到不适合新用户的通知。

谷歌建议使用规范的id来解决这个问题,但我不明白我必须在哪里拦截它并更改数据库中的id。我有一个发送通知的PHP页面:

    $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;
}

我应该在哪里获取canonical_ids并将其插入我的数据库?服务器端还是客户端?我很困惑。

现在,当我向具有多个注册id的设备发送推送通知时,发送请求的php页面的结果是:

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"}]}

我应该怎么做才能不发送错误的通知?

您需要阅读这个工作示例来解决您的答案。基本上,我们必须用现有的重复注册id更新规范id。在响应GCM通知时,有一件事是类似的,即数组的位置有助于用现有的注册id更新标准id,以避免重复消息。