如何在应用程序处于Swift 4运行状态时显示推送通知?

时间:2017-12-25 17:07:02

标签: ios swift3 swift4

虽然直到Swift 3我们使用didReceiveRemoteNotification userInfo: [AnyHashable : Any]方法它的工作正常,但在Swift 4中它不起作用。有没有人对它有任何想法?

1 个答案:

答案 0 :(得分:1)

要在应用处于前台时显示通知,请使用以下方法。

// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler([.alert, .badge, .sound])
}

根据apple documentation,您可以在应用运行时显示通知

enter image description here