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

标签 ios swift3 swift4

尽管在 Swift 3 之前我们使用 didReceiveRemoteNotification userInfo: [AnyHashable : Any] 方法它工作正常,但在 Swift 4 中它不起作用。有人对此有任何想法吗?

最佳答案

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

// 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])
}

据苹果documentation ,您可以在应用程序运行时显示通知

enter image description here

关于ios - 如何在 Swift 4 中应用程序处于运行状态时显示推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970568/

相关文章:

swift - 如何减少类属性的代码重复 (Swift 3)

ios - 如何访问孙子元素?

ios - Swift 4,iOS 如何在添加字符串值后检测标签高度

ios - 如何为 Storyboard中按钮名称的文本设置不同的字体大小

ios - 如何从 iOS 应用程序中删除未使用的 Storyboard?

ios - +[NSObject initialize] 内部的 dispatch_once 是否矫枉过正?

swift - map View 不以当前用户位置为中心

ios - 在Swift 3 NotificationCenter观察器中使用选择器

swift - 识别 Apple TV Remote 上的选择

iOS - 如何在 TableViewController 中设置 TableView 的宽度?