简体   繁体   English

iOS 8远程通知 - 我什么时候应该调用registerForRemoteNotifications()?

[英]iOS 8 Remote notifications - When should I call registerForRemoteNotifications()?

As the apple's official info page about push notification states: 作为关于推送通知状态的苹果官方信息页面:

"Device tokens can change, so your app needs to reregister every time it is launched ." “设备令牌可以更改,因此您的应用每次启动时都需要重新注册。”

I trying to understand what they meant by "every time it is launched". 我试图通过“每次启动”来理解它们的含义。
Does it mean I have to call it in the AppDelegate, in didFinishLaunchingWithOptions() like so: 这是否意味着我必须在AppDelegate中调用它,在didFinishLaunchingWithOptions()中如下所示:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    application.registerForRemoteNotifications()
    return true
}  

Putting this code here will cause it to execute every time the user opens the app, which could be many times a minute if the user is multi tasking between apps (going back and forth between them). 将此代码放在此处将导致它在每次用户打开应用程序时执行,如果用户在应用程序之间进行多任务处理(在它们之间来回切换),则可能每分钟执行多次。

And since calling the registration method invokes an HTTP request to APNS, there is a risk of getting temporary ban. 由于调用注册方法会向APNS调用HTTP请求,因此存在临时禁止的风险。

Are those observations are correct, or I can put the register method like so without any fear? 这些观察结果是否正确,或者我可以毫无畏惧地使用这样的寄存器方法?

(I am using xcode 6.2 with Swift) (我正在使用带有Swift的xcode 6.2)

didFinishLaunchingWithOptions is not called every time the user switches to your app, as often your app is still running. 每次用户切换到您的应用程序时都不会调用didFinishLaunchingWithOptions ,因为您的应用程序通常仍在运行。 What you're describing sounds more like applicationDidBecomeActive . 您所描述的内容听起来更像是applicationDidBecomeActive

Add some NSLog s to both methods to convince yourself that didFinishLaunchingWithOptions is the right place to call .registerForRemoteNotifications . 向两个方法添加一些NSLog ,以说服自己didFinishLaunchingWithOptions是调用.registerForRemoteNotifications的正确位置。

Answering to your exact question:- you can put the register method in didFinishLaunchingWithOptions without any fear. 回答你的确切问题: - 你可以毫不畏惧地将注册方法放在didFinishLaunchingWithOptions中。 It is recommended by apple itself. 苹果本身推荐它。

Case 1:- If in your plist "Application does not run in background" is set to NO, didFinishLaunchingWithOptions will not be called every time the app opens. 情况1: - 如果在您的plist中“应用程序未在后台运行”设置为NO,则每次应用程序打开时都不会调用didFinishLaunchingWithOptions。 It will be called if the app launches completely from a terminated state. 如果应用程序完全从终止状态启动,则会调用它。 Only applicationDidBecomeActive will be called whenever the app launches from background state. 只要应用程序从后台状态启动,就会调用applicationDidBecomeActive。 No need to put register method in applicationDidBecomeActive. 无需在applicationDidBecomeActive中添加register方法。

Case 2:- And if in your plist "Application does not run in background" is set to YES, didFinishLaunchingWithOptions will be called every time the app opens. 情况2: - 如果你的plist“应用程序不在后台运行”设置为YES,则每次应用程序打开时都会调用didFinishLaunchingWithOptions。 you can put the register method in didFinishLaunchingWithOptions without any fear. 你可以毫不畏惧地将注册方法放在didFinishLaunchingWithOptions中。

According to the Apple Documentation for registerForRemoteNotifications() , 根据registerForRemoteNotifications()Apple文档

After successful APNs registration, the app object contacts APNs only when the device token has changed; 注册成功APN后,app对象只有在设备令牌发生变化时才会联系APN; otherwise, calling the registerForRemoteNotifications method results in a call to the application:didRegisterForRemoteNotificationsWithDeviceToken: method which returns the existing token quickly. 否则,调用registerForRemoteNotifications方法会导致调用应用程序:didRegisterForRemoteNotificationsWithDeviceToken:方法,它会快速返回现有标记。

Calling the registration method does not invoke an HTTP request to APNS if it is not necessary. 如果没有必要,调用注册方法不会向APNS调用HTTP请求。

This StackOverflow Comment confirms this empirically: 这个StackOverflow评论凭经验证实了这一点:

You can test this by putting your phone into airplane mode before starting your app. 您可以在启动应用程序之前将手机置于飞行模式进行测试。 If I recall correctly, you'll still get a response with a token, because at some point in the past the device will have negotiated with Apple's server what its token is, and that token doesn't change just because you're currently offline. 如果我没记错的话,你仍会得到一个带有令牌的回复,因为在过去的某个时刻,设备会与Apple的服务器协商它的令牌是什么,并且该令牌不会因为你当前离线而改变。

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

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