0

いくつかのパラメーターを渡して設定する関数があります。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
} 
4

1 に答える 1