简体   繁体   English

如何将对象与通知存储在一起,以便在收到通知时可以调用它?

[英]How to store an object together with a notification so that I can call it when receiving the notification?

I am new to programming and this is my very first project.我是编程新手,这是我的第一个项目。 I am making a pretty simple reminder app;我正在制作一个非常简单的提醒应用程序; I've created a class reminder with the properties .moreInformation (String), .fireDate (Date), .fromDate (Date), .title (String) and .image(UIImage).我创建了一个带有属性 .moreInformation (String)、.fireDate (Date)、.fromDate (Date)、.title (String) 和 .image(UIImage) 的类提醒。 You can edit all these properties within in the application.您可以在应用程序中编辑所有这些属性。 My problem is just: I need a proper solution for storing this object 'reminder'.我的问题只是:我需要一个适当的解决方案来存储这个对象“提醒”。 I am using UserNotifications to register my notification like that:我正在使用 UserNotifications 来注册我的通知:

reminder.fireDate = date
    reminder.image = image
    reminder.description = descriptionTextView.text
    reminder.title = titleTextView.text
    reminder.savedOndate = savedOnDateString

    let center = UNUserNotificationCenter.current()
    let category = UNNotificationCategory(identifier: "General", actions: [], intentIdentifiers: [], options: .customDismissAction)
    center.setNotificationCategories([category])

    let content = UNMutableNotificationContent()
    let contentText = reminder.savedOnDate
    content.title = "Reminder"
    content.body = "Your Reminder from the \(contentText) has arrived!"

    let date2 = reminder.fireDate
    var components = Calendar.current.dateComponents(in: TimeZone.current, from: date2)

    components.hour = 18
    components.minute = 0

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

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


    center.add(request) { (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }

When the user has received the notification, he should see a pop-up view when starting the app, leading him to a separate view controller that shows the description text, the fromDate etc.当用户收到通知时,他应该在启动应用程序时看到一个弹出视图,引导他到一个单独的视图控制器,显示描述文本、fromDate 等。

But how do I store the object together with the notification so when the notification has arrived, the other view controller shows the correct description text/ title etc.?但是如何将对象与通知存储在一起,以便在通知到达时,另一个视图控制器显示正确的描述文本/标题等?

You can use the userInfo dictionary of your UNMutableNotificationContent instance to store custom information.您可以使用UNMutableNotificationContent实例的userInfo字典来存储自定义信息。 Keep in mind that objects stored in the userInfo dictionary must be property-list types.请记住,存储在userInfo字典中的对象必须是属性列表类型。 This means you either need to convert your reminder object to such a type (eg NSDictionary ) or, and this would be a cleaner solution, implement NSCoding in the class of the reminder object and en/decode it using NSKeyedArchiver / NSKeyedUnarchiver .这意味着您要么需要将您的reminder对象转换为这样的类型(例如NSDictionary ),或者,这将是一个更清晰的解决方案,在reminder对象的类中实现NSCoding并使用NSKeyedArchiver / NSKeyedUnarchiver对其进行NSKeyedUnarchiver /解码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序处于状态背景或被杀死时,如何在不点击通知的情况下存储 iOS 远程通知? - How can i store iOS remote notification when app in state background or killed, and without tap the notification? 我在哪里以及如何注册接收通知的对象? - Where and how do I register an object for receiving a Notification? 即使应用程序处于后台或终止状态,我该如何采取措施接收远程通知? - How can I do an action on receiving a remote notification even when the app is in background or terminated? 如何在ios中一起使用本地通知和推送通知? - can I use local notification and push notification together in ios? 收到静默推送通知时调用Web服务? - Call Web Service when receiving silent push notification? 如何在UserDefault中存储推送通知警报消息? - How can I store Push Notification alert message in UserDefault? 如何在swift中接收推送通知时进行服务器调用? - How to make a server call on receiving push notification in swift? 如何在 flutter 后台收到本地通知时调用 function - How can I call a function on local notification received in background in flutter 收到通知后,如何在应用程序图标点击上获取有效载荷? - After Receiving Notification how can i can get payload on app icon click? iOS 9 Swift 2.0如何获取通知,以便当用户向另一用户发送消息时得到通知? - iOS 9 Swift 2.0 How can I get notification so when a user sends a message to another user they get notified?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM