gpt4 book ai didi

android - Google Cloud Messaging 注册 ID 过期

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:51 25 4
gpt4 key购买 nike

我正在为我的 Android 应用程序使用 Google Cloud Messaging,我试图了解注册 ID 何时过期。来自 this发布我能够理解谷歌倾向于在某个时间刷新 ID。我很好奇我的应用程序如何知道 id 何时刷新?如果 Google 决定刷新 ID,而我的服务器直到将消息发送到旧 ID,我认为消息不会被发送。那么我是否必须每次都尝试注册并查看 ID 是否相同?

还有同一篇文章说,当应用程序版本更改时,id 会被刷新,但是通过 list 更改版本时,注册 id 没有改变。那么尝试再次注册版本更改有什么意义呢?

编辑 这是服务器端。规范 ID 究竟存储在哪里?

服务器端代码:

<?php
// Message to be sent
$message = $_POST['message'];
 
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
 
$fields = array(
                'registration_ids'  => array($_POST['registrationIDs']),
                'data'              => array( "message" => $message ),
                );
 
$headers = array(
                    'Authorization: key=' . $_POST['apiKey'],
                    'Content-Type: application/json'
                );
 
// Open connection
$ch = curl_init();
 
// Set the url, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields ) );
 
// Execute post
$result = curl_exec($ch);
 
// Close connection
curl_close($ch);
 
echo $result;
 
?>

最佳答案

I am trying to understand when the registration id expires

GCM 服务器通常会刷新(因此您的旧注册 ID 已过期)注册 ID。任何地方都没有记录这种情况发生的确切时间。但是当发生这种情况时,您会收到通知。

我们如何收到通知?

When you send a notification to a registration id which is expired, for the 1st time the message(notification) will be delivered but you will get a new registration id with the name canonical id. Which mean, the registration id you sent the message to has been changed to this canonical id, so change it on your server side as well.


If Google decides to refresh the ID and my server is till sending the message to the old ID I dont think the message will get sent

就像我说的,它会在第一次发送,即使在过期之后也是如此。


Also the same post says that the id would get refreshed when the app version changes

我不认为这肯定会发生。但即使它发生(注册 ID 更改)与上面解释的情况相同,所以您只需要处理好 canonical id

编辑

$result = curl_exec($ch);
$result = json_decode($result);
$canonical_ids_count = $result->canonical_ids;
if($canonical_ids_count){
//update your DB by replacing the registration id with
//the canonical id(new registration id)
}

关于android - Google Cloud Messaging 注册 ID 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18200167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com