gpt4 book ai didi

ios - 在 UNNotification 我得到的 userInfo 值与 UNNotificationRequest Swift 中的设置值不同

转载 作者:行者123 更新时间:2023-11-29 05:55:14 26 4
gpt4 key购买 nike

我有一个函数可以设置 UNNotificationRequest传入一些参数后,我将这些参数放入 userInfo 中这样我就可以取回它们并在打开本地通知时使用它们。它的打印显示我正在分配传入的正确值。问题是当我打印 userInfo 时我得到了一些其他值在applicationDidBecomeActive 。我不明白为什么会发生这种情况,你能发现我在这里做错了什么吗?

这是function设置通知并输出打印:

func setRouteNotification(_ date: Date, onWeekdaysForNotify weekdays:[Int], snoozeEnabled:Bool,  onSnooze: Bool, soundName: String, routeName: String, index: Int) {

configureCategory()
// Notification content
let routeCheckNotificationContent = UNMutableNotificationContent()
let datesForNotification = correctDate(date, onWeekdaysForNotify: weekdays)
// routeCheckNotificationContent.title = "Hello!! Are you ready to cycle?"
routeCheckNotificationContent.title = "Ciao!! Pronto ad andare?"
// routeCheckNotificationContent.body = "Check route for alerts?"
routeCheckNotificationContent.body = "Controlliamo se ci sono ostacoli lungo il tuo percorso?"
routeCheckNotificationContent.categoryIdentifier = Id.notificationCategory
routeCheckNotificationContent.sound = UNNotificationSound.init(named: soundName + ".mp3")
print(" set notification is: date: \(date), onWeekdaysForNotify : \(weekdays), snoozeEnabled : \(snoozeEnabled), onSnooze: \(onSnooze), soundName: \(soundName), routeName: \(routeName), index: \(index)")

let repeating: Bool = !weekdays.isEmpty
// routeCheckNotificationContent.userInfo = ["snooze" : snoozeEnabled, "index": index, "soundName": soundName, "routeName": routeName, "repeating" : repeating]
routeCheckNotificationContent.userInfo = ["posticipa" : snoozeEnabled, "indice": index, "Suono": soundName, "Titolo percorso" : String(describing: routeName), "repeating" : repeating]
print("routeCheckNotificationContent.userInfo at setting notification is: \(routeCheckNotificationContent.userInfo)")
//repeat weekly if repeat weekdays are selected
//no repeat with snooze notification
if !weekdays.isEmpty && !onSnooze{
}
syncAlarmModel()
var counter = 0
for d in datesForNotification {
if onSnooze {
alarmModel.alarms[index].date = Scheduler.correctSecondComponent(date: alarmModel.alarms[index].date)
}
else {
alarmModel.alarms[index].date = d
}
//trigger
let components = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,], from: d)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

// Notification Request
let uuid = UUID().uuidString
let routeNotificationRequest = UNNotificationRequest(identifier: "Route Notification Request\(uuid)", content: routeCheckNotificationContent, trigger: trigger)
// Add request
UNUserNotificationCenter.current().add(routeNotificationRequest) { (Error) in
if Error != nil {
// print("something went wrong with adding notification")
}
}
// print("added request\(uuid)")
scheduledNotifications.append(counter)
// print("scheduled notifications are \(scheduledNotifications.count)")
counter = ( counter + 1 )
}
// print(alarmModel.alarms)
// print("dates for notification are: \(datesForNotification)")
}

输出打印:

routeCheckNotificationContent.userInfo at setting notification is: [AnyHashable("repeating"): true, AnyHashable("indice"): 0, AnyHashable("Titolo percorso"): "aaa", AnyHashable("Suono"): "Zen", AnyHashable("posticipa"): false]

但是在 AppDelegate :

func applicationDidBecomeActive(_ application: UIApplication) {

let center = UNUserNotificationCenter.current()

center.getDeliveredNotifications { (receivedNotifications) in
for notification in receivedNotifications {
let content = notification.request.content
print(" Body \(content.body)")
print(" Title \(content.title)")
let rotta = content.userInfo["Titolo percorso"]
print("rotta is: \(rotta)")

print(content.userInfo as NSDictionary)
}
}
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// audioPlayer?.play()
// alarmScheduler.checkNotification()
}

输出打印:

Body Controlliamo se ci sono ostacoli lungo il tuo percorso?
Title Ciao!! Pronto ad andare?
rotta is: Optional(Scegli un percorso)
{
Suono = Suono;
"Titolo percorso" = "Scegli un percorso";
indice = 0;
posticipa = 0;
repeating = 0;
}

正文和标题是正确的,但其余部分不正确......我在哪里缺少这些错误值的连接?像往常一样非常感谢

更新:

我从设置这些值的每个地方制作了一些新的打印件,它们都是正确的,但在 AppDelegate 中我从这一行得到值: addEditController.segueInfo = SegueInfo(curCellIndex: alarmModel.count, isEditMode: false, label: "Percorso", mediaLabel: "Suono", mediaID: "", routeLabel: "Scegli un percorso", repeatWeekdays: [], enabled: false, snoozeEnabled: false)SegueInfo 只是一个保存值的结构,直到它们被实际选择的值替换,并且我确实更新它们..

struct SegueInfo {
var curCellIndex: Int
var isEditMode: Bool
var label: String
var mediaLabel: String
var mediaID: String
var routeLabel: String
// var routeId: String
var repeatWeekdays: [Int]
var enabled: Bool
var snoozeEnabled: Bool
}

最佳答案

终于找到问题所在了。我没有得到响应中的值。因此,我从 applicationDidBecomeActive 中删除了代码,并在适当的操作中从 didReceive response 中获取了值,它现在按预期工作。我希望这会帮助其他人。整个功能是:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

switch response.actionIdentifier
{
case Id.checkActionIdentifier:
print("Check tapped")

let content = response.notification.request.content
let rotta = content.userInfo["Titolo percorso"] as! String
RoutesArray.routeName = rotta
print("rotta is: \(rotta)")
print(" Body \(content.body)")
print(" Title \(content.title)")

NewMapViewController.checkCounter = 1

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UINavigationController = storyboard.instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()

let vc = initialViewController.viewControllers[0]
vc.performSegue(withIdentifier: "alertToMapSegue", sender: nil)

case Id.waitActionIdentifier:
print("Wait tapped")

default:
print("Other Action")
}

completionHandler()
}

关于ios - 在 UNNotification 我得到的 userInfo 值与 UNNotificationRequest Swift 中的设置值不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55236156/

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