iOS 8远程通知-什么时候应该调用registerForRemoteNotifications()? [英] iOS 8 Remote notifications - When should I call registerForRemoteNotifications()?

查看:660
本文介绍了iOS 8远程通知-什么时候应该调用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."

我试图理解每次启动"的含义.
这是否意味着我必须在AppDelegate中的didFinishLaunchingWithOptions()中调用它,就像这样:

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:

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).

由于调用注册方法会调用对APNS的HTTP请求,因此存在被临时禁令的风险.

And since calling the registration method invokes an HTTP request to APNS, there is a risk of getting temporary ban.

这些观察是正确的,还是我可以放心地像这样注册方法?

Are those observations are correct, or I can put the register method like so without any fear?

(我在Swift中使用的是xcode 6.2)

(I am using xcode 6.2 with Swift)

推荐答案

didFinishLaunchingWithOptions,因为您的应用通常仍在运行.您所描述的听起来更像是applicationDidBecomeActive.

didFinishLaunchingWithOptions is not called every time the user switches to your app, as often your app is still running. What you're describing sounds more like applicationDidBecomeActive.

在这两种方法中都添加一些NSLog,以使自己确信didFinishLaunchingWithOptions是调用.registerForRemoteNotifications的正确位置.

Add some NSLogs to both methods to convince yourself that didFinishLaunchingWithOptions is the right place to call .registerForRemoteNotifications.

这篇关于iOS 8远程通知-什么时候应该调用registerForRemoteNotifications()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆