gpt4 book ai didi

swift - 如何使用 swift 3 将通知详细信息检索到各个数据元素中

转载 作者:行者123 更新时间:2023-11-30 12:37:52 25 4
gpt4 key购买 nike

我正在尝试读取应用程序收到的所有通知并将其加载到 Collection View 中。希望能够单独检索标题、正文......。我的计数返回正常,但 content.body 和 content.title 为空。可以返回 notification.description 但是完整的字符串。我必须手动解析描述吗?

感谢您的帮助

//read all existing notifications

func listNotification() {
UNUserNotificationCenter.current().getDeliveredNotifications(
completionHandler: {receivedNotifications -> () in

print("Received notification #\(receivedNotifications.count)")

for notification in receivedNotifications {
let content = UNMutableNotificationContent()
print(" Body \(content.body)")
print(" Title \(content.title)")
}
}
}

最佳答案

您正在打印常量content的内容,但是您尚未使用正在迭代的通知对其进行初始化,代码中的任何位置都不会读取此变量。

for notification in receivedNotifications {
let content = notification.request.content
print(" Body \(content.body)")
print(" Title \(content.title)")
}

试试这个方法。

getDeliveredNotifications 返回 UNNotifications 的数组,其中每个请求都有一个请求 ( UNNotificationRequest ),并且位于 UNNotificationContent 的实例内:

var title: String

var subtitle: String

var body: String

var badge: NSNumber?

var sound: UNNotificationSound?

var launchImageName: String

var userInfo: [AnyHashable : Any]

var attachments: [UNNotificationAttachment]

关于swift - 如何使用 swift 3 将通知详细信息检索到各个数据元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com