iOS:为推送通知注册设备

标签 ios push-notification

我的应用程序在启动时首先使用 registerForRemoteNotificationTypes 注册推送通知。

我自然希望触发 didRegisterForRemoteNotificationsWithDeviceToken 事件或 didFailToRegisterForRemoteNotificationsWithError

但我注意到,如果用户为我的应用禁用了推送通知,那么这些通知都不会被调用。在这种情况下它不应该失败并触发 didFailToRegisterForRemoteNotificationsWithError 吗?

问题是,我的应用程序会等待推送注册的结果(无论是通过还是失败),然后再继续其余的网络任务。在做任何其他事情之前,我基本上依赖于结果。

我可以使用另一种方法来做到这一点吗?像 didFailToRegisterForRemoteNotificationsAsDisabledForApplication 之类的东西(我编的)??

编辑:

Apple Docs 对 registerForRemoteNotificationTypes 说了以下内容:

当您发送此消息时,设备会启动 Apple 推送服务的注册过程。如果成功,app delegate 在 application:didRegisterForRemoteNotificationsWithDeviceToken: 方法中收到一个设备 token ;如果注册不成功,则通过 application:didFailToRegisterForRemoteNotificationsWithError: 方法通知委托(delegate)人。如果应用委托(delegate)收到设备 token ,它应该与其提供者连接并将 token 传递给它。

因此,如果用户已为我的应用禁用推送通知并且我的代码正在尝试 registerForRemoteNotificationTypes 为什么它不触发 didFailToRegisterForRemoteNotificationsWithError

最佳答案

来自 Technical Note TN2265 :

No Delegate Callbacks

When the first push-capable app is installed, iOS or OS X attempts to establish a persistent network connection to the push service that will be shared by all push-capable apps on the system. If neither delegate callback application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError: is called, that means that this connection has not yet been established.

This is not necessarily an error condition. The system may not have Internet connectivity at all because it is out of range of any cell towers or Wi-Fi access points, or it may be in airplane mode. Instead of treating this as an error, your app should continue normally, disabling only that functionality that relies on push notifications.

Keep in mind that network availability can change frequently. Once the persistent connection to the push service succeeds, one of the previously-mentioned application delegate methods will be called.

On iOS, push notifications use the cellular data network whenever possible, even if the device is currently using Wi-Fi for other network activity such as web browsing or email. However, the push service will fall back to Wi-Fi if cellular data service isn't available.

If your iOS device is capable of using the cellular data network, check that it has an active cellular data plan. Turn off Wi-Fi in Settings and see if you can still browse the web with Safari, for example. On the other hand, if the push service is using Wi-Fi, any firewalls between your device or computer and the Internet must allow TCP traffic to and from port 5223.

来自同一文档的另一个部分:

Resetting the Push Notifications Permissions Alert on iOS

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by following these steps:

  1. Delete your app from the device.
  2. Turn the device off completely and turn it back on.
  3. Go to Settings > General > Date & Time and set the date ahead a day or more.
  4. Turn the device off completely again and turn it back on.

关于iOS:为推送通知注册设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412381/

相关文章:

php - Google Firebase 通知在控制台上工作但不在 API 上工作

ios - 如何使用xib实现UILabel的行间距?

ios - OBD2Kit Swift 示例无法进行类型转换,一直为零

ios - 使用 Firebase 检索多查询

ios - 在 UIViewController 中,我应该在 viewDidLoad 或 viewWillLayoutSubviews 中创建手动自动布局约束吗?

ios - 锁定屏幕后,“首次推送”通知消失

python - PubNub - 推送 HTML 更新

ios - application:didFinishLaunchingWithOptions没有被调用

c# - OneSignal 推送通知 - 不适用于 WP8.1

ios - 在 iOS 10 中实现 "reply to notification from lock screen"