简体   繁体   English

应用程序终止时推送通知

[英]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.

相关问题 应用终止时响应推送通知 - responding to push notification when app is TERMINATED 当应用未运行(终止)时,通知应用有关远程推送通知的信息 - Notify the app about remote push notification when the app is not running (terminated) 应用未运行/应用终止时如何处理推送通知 - How to handle push notification when app is not running/app is terminated 应用未运行/应用终止时如何获取推送通知 - How to get push notification when app is not running/app is terminated 当应用程序在 ios 13 中的静默推送通知中终止时运行代码 - Run code when app is terminated on silent push notification in ios 13 应用终止时处理远程推送通知的声音 - Handling remote push notification sounds when app is terminated FCM iOS 推送通知在应用程序终止时捕获自定义数据? - FCM iOS push notification capture custom data when app is terminated? 如果应用终止,则未收到iOS FCM推送通知 - iOS FCM push notification not received if app is terminated 应用终止时的远程通知 - Remote notification when app is terminated 当应用程序从后台收到推送通知或在iOS中终止时,无法收听来自NSNotificationCenter的通知 - Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM