繁体   English   中英

应用程序终止时推送通知

[英]Push notification when app is terminated

如果应用程序在后台和/或应用程序位于前台,我的应用程序可以正常使用推送通知。

我遇到的问题是如果应用程序被终止(我强制双击主页按钮,找到应用程序,然后向上滑动)。

我正在使用ios 9和swift 2。

在app delegate中,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) {...}

根据文档和此链接 ,为反对旧版本的didReceiveRemoteNotification ,这种方法如果应用程序被终止调用(如反对的通话都会/ DID finishLaunchingWithOptions)。

但是,如果有一个推(已收到 - 我可以在屏幕上看到它)并且我终止后启动应用程序,这个方法似乎不会被称为处理推送的代码(只需发布一个通知所以它被相应的ViewController拾取)不会被调用。

我错过了什么? 我需要在didFinishLaunchingWithOptions中进行额外的检查吗? 别的地方?

管理以解决当ios 9.1终止应用程序时拦截远程推送的问题,但是在9.2(随机失败?)上失败了:

注册远程:

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.

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