本文介绍了本地通知发出声音但不显示(快速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apple的UNUserNotificationCenter为我的应用创建本地通知.

I am trying to create a local notification for my app using Apple's UNUserNotificationCenter.

这是我的代码:

let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent()
content.title = task.name
content.body = task.notes
content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

let request = UNNotificationRequest(identifier: task.UUID, content: content, trigger: trigger)

center.add(request) { (error:Error?) in
   if let theError = error {
      print("Notification scheduling error: \(error)")
   }else{
      print("Notification was scheduled successfully")
   }
}

访问请求:

let center = UNUserNotificationCenter.current()

//request notification access
let options: UNAuthorizationOptions = [.alert, .sound]
center.requestAuthorization(options: options) { (granted, error) in
   if !granted {
      print("Notification access was denied")
   }
}

5秒钟后,该应用发出默认"声音,但不显示警报内容.我正在尝试在前台和后台使用该应用程序.

After 5 seconds the app makes the Default sound but does not show the alert content. I am trying both with the app in the foreground and in the background.

推荐答案

我遇到了同样的问题.

如果内容正文为空白,即(content.body =="),那么即使您有标题,也不会收到通知.

If your content body is blank ie(content.body == "") then you won't get a notification even if you do have a title.

因此解决方案是确保您的内容主体不是空字符串.

So the solution is to make sure that your content body is not an empty string.

这篇关于本地通知发出声音但不显示(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:45