2015-04-03 83 views
0

我已經在我的Android應用程序中實施推送通知。當重新安裝應用程序witouth註銷時GCM和規範ID

現在,我遇到了canonicals 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和我的數據庫插入?服務器端還是客戶端?我很困惑。

+0

在你的代碼中加入一些註釋可以幫助他人回答你的問題。讓標題更多的問題也會有所幫助。 – cmbarbu 2015-04-03 11:46:40

+0

對不起,我編輯了我的問題 – diegocom 2015-04-03 11:56:13

+0

看看SO貼子的答案,可能與你正在查找的東西有關,很多信息包裝在這裏。 http://stackoverflow.com/questions/27072824/how-to-get-canonical-id-from-gcm希望幫助! – AniV 2015-04-03 22:40:26

回答

0

現在,當我發送推送通知到設備與多個註冊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"}]} 

我應該怎麼做,不發送一個錯誤的通知?

0

你需要閱讀這個工作Example來解決你的答案。基本上我們必須用現有的重複註冊ID更新規範id。在GCM通知的迴應中,有一件事情是相似的,即數組的位置有助於使用現有的註冊ID更新規範標識以避免重複的消息。

相關問題