本地通知内容:字幕或正文未显示[Swift 5]

问题描述 投票:1回答:1

我以前在我的应用中使用了本地通知,但只显示标题。现在,我想为每个通知添加一个字幕和正文。我为subtitle添加了bodyUNMutableNotificationContent。但是当通知出现时,字幕或正文不存在。 我该如何解决?

这是我添加本地通知的方式:

        let content      = UNMutableNotificationContent()
        content.title    = "title works!"
        content.subtitle = "testing subTitle"
        content.body = "testing body"
        content.sound    = .default

        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

        let identifier = "notificationId"
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request) { (error) in
            if let error = error {
                print("Hey listen! Error adding notification: \(error.localizedDescription)")
            } else {
                print("saved notification")
            }
        }

这是我要求通知权限的方式:

private func requestNotificationAuthorization() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if let error = error {
                print("Hey listen! Got an error requesting notification authorization: \(error.localizedDescription)")
            } else {

                if granted {
                    DispatchQueue.main.async {
                      UIApplication.shared.registerForRemoteNotifications()
                    }
                } else {
                    print("not granted")
                }
            }
        }
    }

这是我的willPresent代码:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .badge, .sound])
    }
swift xcode notifications alert uilocalnotification
1个回答
0
投票

您是指在前台还是在后台接收本地通知的情况?

如果位于前台,则将使用userNotificationCenter(_:willPresent:withCompletionHandler:),在这种情况下,如果您想要警报,则需要自己处理警报(在代码中)。因此,您可以从userNotificationCenter(_:willPresent:withCompletionHandler:)开始检查代码,以查看是否有仅显示标题的自定义警报。 (您也可以编辑您的信息以向我们显示userNotificationCenter(_:willPresent:withCompletionHandler:)中的内容)。

© www.soinside.com 2019 - 2024. All rights reserved.