[英]Push notification when app is terminated
My app works fine with push notifications if the app was in the background and/or if the app is in the foreground. 如果应用程序在后台和/或应用程序位于前台,我的应用程序可以正常使用推送通知。
The problem I have is if the app is terminated (which I force by double-click on the home button, find the app, and swipe up). 我遇到的问题是如果应用程序被终止(我强制双击主页按钮,找到应用程序,然后向上滑动)。
I am using ios 9 and swift 2. 我正在使用ios 9和swift 2。
In app delegate, didFinishLaunchingWithOptions, I do: 在app delegate中,didFinishLaunchingWithOptions,我这样做:
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
Then: 然后:
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}
Followed by didRegisterForRemoteNotificationsWithDeviceToken & didFailToRegisterForRemoteNotificationsWithError. 其次是didRegisterForRemoteNotificationsWithDeviceToken&didFailToRegisterForRemoteNotificationsWithError。
Then, I am using the relatively new method: 然后,我使用相对较新的方法:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {...}
According to the documentation and this link , as oppose to the old version of didReceiveRemoteNotification
, this method is called if the app was terminated (as oppose to calling will/did finishLaunchingWithOptions). 根据文档和此链接 ,为反对旧版本的
didReceiveRemoteNotification
,这种方法如果应用程序被终止调用(如反对的通话都会/ DID finishLaunchingWithOptions)。
However, if there was a push (which was received - I can see it on the screen) and I launch the app after it has been terminated, this method does not seem to be called as the code that handles the push (simply post a notification so it is picked up by the respective ViewController) does not get called. 但是,如果有一个推(已收到 - 我可以在屏幕上看到它)并且我终止后启动应用程序,这个方法似乎不会被称为处理推送的代码(只需发布一个通知所以它被相应的ViewController拾取)不会被调用。
What am I missing? 我错过了什么? Is there an additional check I need to do in didFinishLaunchingWithOptions?
我需要在didFinishLaunchingWithOptions中进行额外的检查吗? Somewhere else?
别的地方?
Managed to solve the problem of intercepting a Remote Push when the app is terminated for ios 9.1 with the following but it failed on 9.2 (random failure?): 管理以解决当ios 9.1终止应用程序时拦截远程推送的问题,但是在9.2(随机失败?)上失败了:
Register for remote: 注册远程:
if #available(iOS 9, *) {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
// UIApplication.sharedApplication().registerUserNotificationSettings(settings)
//
// UIApplication.sharedApplication().registerForRemoteNotifications()
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else if #available(iOS 8.0, *){
register for 8...
} else { //ios 7
register for 7...
}
if let _ = launchOptions {
if let _ = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
handleRemotePush()
} else if let _ = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
handleLocalNots()
} else {
handleElse()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.