如何在 didfinishlaunchingwithoptions Swift 中调用 didReceiveRemoteNotification?

How to call didReceiveRemoteNotification in didfinishlaunchingwithoptions Swift?

如何调用didfinishlaunchingwithoptions中的didReceiveRemoteNotification方法?我正在使用 swift 语言。 当用户直接打开应用时,通知被取消,didReceiveRemoteNotification 中的操作没有被调用。如何处理?

我已经到达这里了。但是无法完成

if let options = launchOptions, notification = options[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notification, fetchCompletionHandler: _______)

        }

根据文档,

The notification is delivered when the app isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound, and perhaps displaying one or more action buttons for the user to tap.

The user taps a custom action button in an iOS 8 notification. In this case, iOS calls either application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forLocalNotification:completionHandler:. In both methods, you get the identifier of the action so that you can determine which button the user tapped. You also get either the remote or local notification object, so that you can retrieve any information you need to handle the action.

The user taps the default button in the alert or taps (or clicks) the app icon. If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.

For remote notifications, the system also calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method of the app delegate.

If the app icon is clicked on a computer running OS X, the app calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload. If the app icon is tapped on a device running iOS, the app calls the same method, but furnishes no information about the notification.

The notification is delivered when the app is running in the foreground. The app calls the application:didReceiveRemoteNotification:fetchCompletionHandler: or application:didReceiveLocalNotification: method of the app delegate. (If application:didReceiveRemoteNotification:fetchCompletionHandler: isn’t implemented, the system calls application:didReceiveRemoteNotification:.) In OS X, the system calls application:didReceiveRemoteNotification:.

您可以在此处找到相同的 link: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

总结: 你必须在 3 种情况下处理 APNS :

  1. 应用程序在Foreground/App在后台(但未暂停): application:didReceiveRemoteNotification:调用应用程序委托的方法并移交有效负载给你。

2.应用程序 suspended/killed 并且用户点击通知或警报: application:didFinishLaunchingWithOptions: 应用程序委托被调用,您可以从 launchOption 访问负载.

3.应用程序 suspended/killed 并且用户点击应用程序图标:您将不会收到有关 APNS 的任何信息。您可以做的最简单的解决方案是,一旦应用程序激活,就对网络服务器进行网络服务调用,获取所有更新的信息并更新您的 UI 和应用程序徽章计数:)

希望对您有所帮助:)