1

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

问题:- 应用程序在终止模式下收到通知时没有委托呼叫。请建议在这种情况下该怎么做。

4

2 回答 2

1

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

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

只有当应用程序在前台时,才会在委托上调用该方法。如果方法未实现或处理程序未及时调用,则不会显示通知。应用程序可以选择将通知呈现为声音、徽章、警报和/或在通知列表中。该决定应基于通知中的信息是否对用户可见。

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

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

当用户通过打开应用程序、关闭通知或选择 UNNotificationAction 来响应通知时,将在委托上调用该方法。必须在应用程序从 application:didFinishLaunchingWithOptions: 返回之前设置委托。

于 2018-07-04T06:12:29.383 回答
1

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

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
    }
}

PS:仅适用于 iOS 10 或更高版本

于 2018-07-04T06:45:16.650 回答