简体   繁体   English

iOS:为推送通知注册设备

[英]iOS: Registering device for push notifications

My application, upon launch, first registers for push notifications using registerForRemoteNotificationTypes .我的应用程序在启动时首先使用registerForRemoteNotificationTypes注册推送通知。

I naturally expect either the didRegisterForRemoteNotificationsWithDeviceToken event or the didFailToRegisterForRemoteNotificationsWithError to fire.我自然希望didRegisterForRemoteNotificationsWithDeviceToken事件或didFailToRegisterForRemoteNotificationsWithError触发。

But I noticed that if a user has push notifications disabled for my app, then neither one of these get called.但我注意到,如果用户为我的应用禁用了推送通知,那么这些都不会被调用。 Shouldn't it fail in this case and fire didFailToRegisterForRemoteNotificationsWithError ?在这种情况下它不应该失败并触发didFailToRegisterForRemoteNotificationsWithError吗?

The thing is, my app waits for the result of the push registration (whether it passes or fails) before proceeding with the rest of it's networking tasks.问题是,我的应用程序在继续执行其余的网络任务之前等待推送注册的结果(无论是通过还是失败)。 I basically rely on the result before doing anything else.在做任何其他事情之前,我基本上依赖于结果。

Is there another method I can use to do this?我可以用另一种方法来做到这一点吗? Something like a didFailToRegisterForRemoteNotificationsAsDisabledForApplication (I made this up)??didFailToRegisterForRemoteNotificationsAsDisabledForApplication类的didFailToRegisterForRemoteNotificationsAsDisabledForApplication (我编的)??

EDIT:编辑:

Apple Docs say the following about registerForRemoteNotificationTypes : Apple Docs 对registerForRemoteNotificationTypes说了以下内容:

When you send this message, the device initiates the registration process with Apple Push Service.当您发送此消息时,设备会启动 Apple Push Service 的注册过程。 If it succeeds, the app delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken: method;如果成功,应用程序委托会在应用程序中收到一个设备令牌:didRegisterForRemoteNotificationsWithDeviceToken: 方法; if registration doesn't succeed, the delegate is informed via the application:didFailToRegisterForRemoteNotificationsWithError: method.如果注册不成功,则通过 application:didFailToRegisterForRemoteNotificationsWithError: 方法通知委托人。 If the app delegate receives a device token, it should connect with its provider and pass it the token.如果应用程序委托收到设备令牌,它应该与其提供者连接并将令牌传递给它。

So if the user's has push notifications disabled for my app and my code is trying to registerForRemoteNotificationTypes why doesn't it fire didFailToRegisterForRemoteNotificationsWithError ?因此,如果用户为我的应用程序禁用了推送通知,并且我的代码正在尝试registerForRemoteNotificationTypes为什么不触发didFailToRegisterForRemoteNotificationsWithError

From Technical Note TN2265 :来自技术说明 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.安装第一个支持推送的应用程序后,iOS 或 OS X 会尝试与推送服务建立持久的网络连接,该连接将由系统上的所有支持推送的应用程序共享。 If neither delegate callback application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError: is called, that means that this connection has not yet been established.如果委托回调 application:didRegisterForRemoteNotificationsWithDeviceToken: 和 application:didFailToRegisterForRemoteNotificationsWithError: 均未调用,则表示此连接尚未建立。

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.该系统可能根本没有互联网连接,因为它超出了任何蜂窝塔或 Wi-Fi 接入点的范围,或者它可能处于飞行模式。 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.在 iOS 上,推送通知会尽可能使用蜂窝数据网络,即使设备当前正在使用 Wi-Fi 进行其他网络活动,例如网页浏览或电子邮件。 However, the push service will fall back to Wi-Fi if cellular data service isn't available.但是,如果蜂窝数据服务不可用,推送服务将回退到 Wi-Fi。

If your iOS device is capable of using the cellular data network, check that it has an active cellular data plan.如果您的 iOS 设备能够使用蜂窝数据网络,请检查它是否具有有效的蜂窝数据计划。 Turn off Wi-Fi in Settings and see if you can still browse the web with Safari, for example.例如,在“设置”中关闭 Wi-Fi,看看您是否仍然可以使用 Safari 浏览网页。 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.另一方面,如果推送服务使用 Wi-Fi,则您的设备或计算机与 Internet 之间的任何防火墙都必须允许进出端口 5223 的 TCP 流量。

From another section in the same document:来自同一文档的另一部分:

Resetting the Push Notifications Permissions Alert on iOS在 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.启用推送的应用程序首次注册推送通知时,iOS 会询问用户是否希望接收该应用程序的通知。 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.再次完全关闭设备并重新打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM