【发布时间】:2016-03-25 19:20:09
【问题描述】:
如果应用程序在后台和/或如果应用程序在前台,我的应用程序可以很好地处理推送通知。
我遇到的问题是应用程序是否终止(我通过双击主页按钮,找到应用程序并向上滑动来强制终止)。
我正在使用 ios 9 和 swift 2。
在应用委托中,didFinishLaunchingWithOptions,我这样做:
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
然后:
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}
之后是 didRegisterForRemoteNotificationsWithDeviceToken 和 didFailToRegisterForRemoteNotificationsWithError。
那么,我用的是比较新的方法:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {...}
根据文档和这个link,与旧版本的didReceiveRemoteNotification相反,如果应用程序被终止,则调用此方法(与调用will/did finishLaunchingWithOptions相反)。
但是,如果有推送(已收到 - 我可以在屏幕上看到)并且我在应用程序终止后启动应用程序,则似乎不会调用此方法作为处理推送的代码 (只需发布一个通知,以便相应的 ViewController 接收它)不会被调用。
我错过了什么?我需要在 didFinishLaunchingWithOptions 中进行额外检查吗?其他地方?
【问题讨论】:
标签: ios push-notification apple-push-notifications swift2