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

查看:32
本文介绍了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天全站免登陆