Php 重新安装未注销的应用程序时的GCM和规范ID
我已经在我的Android应用程序中实现了推送通知 现在,我遇到了著名的规范id问题。 如果我在不注销的情况下卸载应用程序,也不从数据库中删除Php 重新安装未注销的应用程序时的GCM和规范ID,php,android,notifications,google-cloud-messaging,push,Php,Android,Notifications,Google Cloud Messaging,Push,我已经在我的Android应用程序中实现了推送通知 现在,我遇到了著名的规范id问题。 如果我在不注销的情况下卸载应用程序,也不从数据库中删除设备\u id,当我重新安装应用程序时,我会收到不针对新用户的通知 Google建议使用规范id来解决这个问题,但我不知道我必须在哪里拦截它并更改数据库中的id。 我有一个发送通知的PHP页面: $gcm=new GCM(); //get the array of all id associated to the user $row
设备\u id
,当我重新安装应用程序时,我会收到不针对新用户的通知
Google建议使用规范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;
}
我应该在哪里获取规范ID并将其插入数据库?服务器端还是客户端?我很困惑。现在,当我向具有多个注册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以避免重复消息。在代码中添加一些注释将有助于其他人回答您的问题。让标题更像问题也会有帮助。对不起,我已经编辑了我的问题,看看SO帖子的答案,可能与你要找的有关,这里有很多信息。希望有帮助!!谢谢你提供的信息。然后我必须在服务器端获取规范ID,并覆盖我在数据库中请求的旧注册ID?但是这样客户端总是在最后一次收到一个错误的请求,对吗?