如何以编程方式确定推送通知状态?
在我的应用程序中,第一次推送通知注册时,我调用 didRegisterForRemoteNotificationsWithDeviceToken
并持久保存设备令牌,并更新设备令牌的服务器列表。现在,如果有人从 iPhone 设置中关闭推送通知设置,我如何从我的应用程序中确定它,以便我也可以从服务器中删除设备令牌。我知道 APNS 提供了一个反馈列表,但除此之外,有没有办法以编程方式在 App 中确定它?感谢您的帮助!
In my app, for first time push notification registration, I call didRegisterForRemoteNotificationsWithDeviceToken
and save the device token in persistence as well as update my server list for device token. Now afterwards if somebody turns the push notification settings off from iPhone Settings how can I determine it from my app so that I can remove the device token from server as well. I know APNS provides a feedback list, but other than that is there a way to determine it in App programmatically? Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您不想以这种方式管理代币。
您的应用程序应始终向 Apple 请求 APNs 令牌。您应该始终将该令牌发送到您自己的服务器,可能会将令牌与您的用户相关联(如果您有的话)。您这样做是因为令牌可能发生变化,因此您需要确保始终拥有最新的令牌。
反馈服务将告诉您(实际上,您可以按照您选择的某个时间间隔对其进行轮询)哪些令牌已变得无效。此时,您从服务器端数据库中删除令牌。需要明确的是,您需要一个服务器端进程来轮询 Apple 的反馈服务,然后更新服务器端数据库。
在您尝试使用令牌发送通知之前,您不会收到有关无效令牌的反馈。当您发送通知时(我相信)Apple 会接受该通知,但是当 Apple 发现它是针对无效令牌时,该消息将被删除,并且该令牌将添加到您的反馈中。
现在,如果您的应用程序的用户在您的应用程序首次询问时接受推送通知,但后来通过您的应用程序的“设置”应用程序关闭了通知,您将不会收到任何有关它的反馈。据我所知,发生的情况是,您发送到该设备的任何通知都将发送到该设备,但操作系统会丢弃它,尊重用户在“设置”应用程序中对您的应用程序和通知的最终选择。
最后,您可以在应用程序中调用一个 API,以获取设备上为您的应用程序启用了哪些类型的通知的位掩码。这是我为此目的编写的方法;根据需要调整:
但我不建议使用它来决定您的应用程序是否应该告诉您的服务器从数据库中删除令牌。这不是整个 APNs 系统的工作方式……我相信。
I believe you do not want to manage tokens this way.
Your app should always be asking Apple for an APNs token. You should always then send that token to your own server, likely associating the token with your user (if you have one). You do this because the token could change, so you want to make sure you always have up-to-date tokens.
The Feedback service will tell you (actually, you poll it at some interval of your choosing) which tokens have become invalid. At this point, you remove the tokens from your server-side database. To be clear, you need a server-side process that polls Apple's feedback service and then updates your server-side database.
You will not receive feedback about invalid tokens until you try to send a notification using the token. The notification will (I believe) be accepted by Apple when you send it, but when Apple discovers it's for an invalid token, the message is dropped, and the token is added to your feedback.
Now, if the user of your app accepts push notifications when your app first asks about it, but later turns off notifications via the Settings app for your app, you will not get any feedback about it. What happens, near as I can tell, is that any notification you send to that device will be sent to the device, but the OS drops it, honoring the user's ultimate choice in the Settings app for your app and notifications.
Finally, there is an API you can call in your app to get a bitmask of which kinds of notifications are enabled for your app on the device. Here's a method I wrote for this purpose; adjust as needed:
But I would not recommend using this to decide if you app should tell your server to delete the token from your database. That's not how the whole APNs system is intended to work... I believe.