如何删除 Apple APNS 反馈收到的设备令牌

发布于 2024-08-23 15:23:41 字数 151 浏览 4 评论 0原文

我成功通过 PHP 获取 Apple APNS 反馈数据。我得到的结构(经过一些处理)看起来像这样:

时间戳

设备令牌

我的问题是如何知道我应该从数据库中删除哪些设备令牌并停止向它们发送通知。

问候,

姆拉乔

I am successfully fetching Apple APNS feedback data via PHP. The structure that I am getting (after some processing) looks something like this:

timestamp

device token

My question is how to know which of the device tokens should I remove from my database and stop sending notifications to them.

Regardz,

Mladjo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一身骄傲 2024-08-30 15:23:41

时间戳是这里的关键元素。 Apple 发送的时间戳表示推送服务上次尝试向设备发送消息并发现应用程序被卸载的时间。如果此后该设备已重新注册到您的服务,则无需将其删除。

因此,每次您的应用程序加载并将令牌发送到您的服务时,您都应该在数据存储中记录时间。当您运行反馈时,您应该检查来自 Apple 的时间,并将其与您上次从设备上的应用程序收到更新的时间进行比较。如果 Apple 发送的时间比您收到更新的时间更新,那么您应该删除(或禁用)该设备。如果 Apple 的时间较早,则无需删除它,因为自 Apple 上次尝试交付以来,用户已重新安装了设备。

The timestamp is the crucial element here. The timestamp sent by Apple indicates the last time the push service attempted to deliver a message to the device and found the app to be uninstalled. If the device has re-registered with your service since then there is no need to delete it.

Therefore, every single time your app loads and sends the token to your service, you should log the time in your data store. When you run feedback you should check the time from Apple and compare it to the time you last received an update from the app on the device. If the time Apple sends is newer then the time you received an updated then you should delete (or disable) the device. If the time from Apple is earlier then you do not need to delete it because the user has reinstalled the device since Apple last tried to deliver.

橘和柠 2024-08-30 15:23:41

反馈给出的所有设备均“失败”,应删除。没有反馈意味着不应删除任何设备。 Apple 文档对此进行了介绍:

Apple APN 文档

All devices given by feedback are 'failed' and should be removed. No feedback means no devices should be removed. It's covered over on the Apple Documentation:

Apple APN Documentation

留蓝 2024-08-30 15:23:41

简洁的解释@argon,但是我还有关于时间戳的另一个问题。

每次应用程序启用推送通知时,设备令牌都会发送到服务器。我是否应该从服务器获取时间戳与设备令牌一起保存,因为 didRegisterForRemoteNotificationsWithDeviceToken 只提供 deviceToken 而不是时间。如果我的服务器在不同时区运行并且 APNS 在不同时区运行,则无法将存储的注册时间(以及设备令牌)与从 APNS 反馈收到的时间戳进行比较,以检查注册顺序 -> 。卸载->重新注册。

我假设 APNS 反馈时间戳采用 UTC 格式,并且服务器与设备令牌一起存储的时间戳必须在存储 ISO 时间戳之前转换为 UTC。这样,时间戳将位于同一时区,并且差异检查将保持一致。

请澄清

neat explanation @argon, however I have another question about the timestamp.

Every time when an app enables push notification, the device token is sent to the server. Should I taken the timestamp from my server as to be persisted along with device token as didRegisterForRemoteNotificationsWithDeviceToken only gives deviceToken and not time. If my server runs in different timezone and APNS is running at different timezone, then the registration time stored( along with device token) cannot be compared with timestamp received from APNS feedback to check the sequence of register -> uninstall -> reregister.

I presume the APNS feedback timestamp is in UTC and the timestamp the server stores along with device token has to be converted to UTC before storing the ISO timestamp. This way both the timestamp will be in same timezone and diff check will be consistent.

please clarify

箹锭⒈辈孓 2024-08-30 15:23:41

@fyasar,

所以您的建议是根据设备 ID(或某个密钥)存储设备令牌。当收到设备令牌的反馈时,从数据库中删除该设备令牌行,对吗?如果理解正确,那么在用户安装应用程序、卸载应用程序然后再次安装应用程序的情况下,这将不起作用,所有这些都在短时间内完成,并且只有在这一切发生后才查询反馈服务。在这种情况下,如果不考虑反馈中的时间戳,设备令牌将被删除,这是不正确的,因为用户已再次安装应用程序并重新注册推送通知。

我的问题是,正如苹果文档和许多博客中所建议的那样,在注册时,当设备令牌被持久化时,时间戳必须随之被持久化。应保留哪个时区的 ISO 时间,或者反馈服务返回时间戳的时区是什么。

@fyasar,

So your recommendation is to store the device token against device id(or some key). When a feedback is received for a device token, remove that device token row from DB, right? If have understood right, that wouldn't work in scenario were a user installs app, uninstalls it and then installs it again all with in a short duration and the feedback service was queried only after all this happened. In this case, if timestamp in feedback is not considered, device token will be removed which is incorrect as user has again installed the app and reregistered for push notification.

My question is this, as suggested in apple doc and many blogs, on registration, when device token is persisted, timestamp has to be persisted along with it. What time zone's ISO time should be persisted or what is the time zone on which feedback service returns the timestamp.

野味少女 2024-08-30 15:23:41

时间戳(作为四字节 time_t 值),指示 APNs 的时间
确定该应用程序不再存在于设备上。这个值,
按网络顺序排列,表示自午夜 12:00 以来的秒数
世界标准时间 1970 年 1 月 1 日。

您可以将其与表的上次插入时间进行比较,然后从数据库中删除无效令牌,在我的情况下,我使用 mysql 和 php 作为服务器端,

$sql="SELECT insert_time from device_tokens ORDER BY insert_time DESC LIMIT 1";

它将从数据库返回上次更新时间,然后我只需使用以下命令将其转换为纪元时间戳

$sql1="SELECT UNIX_TIMESTAMP(' $timestamp')";

最后我只是将它与 apns 反馈时间戳进行比较,如下所示

if($inactive_Timestamp>$dbTime_stamp)
  {
foreach ($apnsfeedback_tokens as $key => $value) {
  # code...

  $inactive_Token=$value['devtoken'];

 $sql= "DELETE FROM device_tokens WHERE device_token='$inactive_Token'";


if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

A timestamp (as a four-byte time_t value) indicating when APNs
determined that the app no longer exists on the device. This value,
which is in network order, represents the seconds since 12:00 midnight
on January 1, 1970 UTC.

you can compare it with your table's last insert time and then remove the invalid token from db, In my case i am using mysql and php for sever side

$sql="SELECT insert_time from device_tokens ORDER BY insert_time DESC LIMIT 1";

it will return last updated time from db and then i just convert it into epoch timestamp by using

$sql1="SELECT UNIX_TIMESTAMP(' $timestamp')";

and finally i just compare it with apns feedback timestamp like this

if($inactive_Timestamp>$dbTime_stamp)
  {
foreach ($apnsfeedback_tokens as $key => $value) {
  # code...

  $inactive_Token=$value['devtoken'];

 $sql= "DELETE FROM device_tokens WHERE device_token='$inactive_Token'";


if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}
無處可尋 2024-08-30 15:23:41

您应该存储设备及其设备令牌数据,然后您可以根据其设备令牌找到这些设备。您可以使用设备令牌来识别每个设备。很容易找到它们的状态并将其更改到您的数据库中。

You should store devices with their device token data, and then you can find these devices according to their device tokens. You might use device token for identifier each device. Than would be easy to find and change their statuses into your db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文