当远程通知到达时哪个委托首先调用,而我的应用程序处于终止模式(不在后台)

Which delegate called first when remote notification arrived, while my app is in killed mode(not in background)

动机:- 我想存储当应用程序未处于后台模式或被终止模式时收到通知时存储的通知有效负载。

问题:- 当应用程序在被杀死时收到通知时没有委托调用 mode.Please 建议在这种情况下该怎么做。

来自 Apple 文档(UNUserNotificaitonCenter 框架 iOS 10+)...

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("didReceive \(response.notification.request.content.userInfo)")
}

The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    print("willPresent: \(notification.request.content.userInfo)")
    completionHandler([.alert, .badge, .sound])
}

The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.

当应用程序被杀死或退出时,你不能这样做。但是您可以检索已发送的通知并在应用程序再次打开时处理它们。您可以使用以下流程获取通知。

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    for aNoitfication in notifications
    {
        let payload = aNoitfication.request.content.userInfo
        //process the payload
    }
    DispatchQueue.main.sync { /* or .async {} */ 
        // update UI
    }
}

P.S:仅适用于 iOS 10 或更高版本