![](/img/trans.png)
[英]How can i store iOS remote notification when app in state background or killed, and without tap the notification?
[英]How to store an object together with a notification so that I can call it when receiving the notification?
我是編程新手,這是我的第一個項目。 我正在制作一個非常簡單的提醒應用程序; 我創建了一個帶有屬性 .moreInformation (String)、.fireDate (Date)、.fromDate (Date)、.title (String) 和 .image(UIImage) 的類提醒。 您可以在應用程序中編輯所有這些屬性。 我的問題只是:我需要一個適當的解決方案來存儲這個對象“提醒”。 我正在使用 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)
}
}
當用戶收到通知時,他應該在啟動應用程序時看到一個彈出視圖,引導他到一個單獨的視圖控制器,顯示描述文本、fromDate 等。
但是如何將對象與通知存儲在一起,以便在通知到達時,另一個視圖控制器顯示正確的描述文本/標題等?
您可以使用UNMutableNotificationContent
實例的userInfo
字典來存儲自定義信息。 請記住,存儲在userInfo
字典中的對象必須是屬性列表類型。 這意味着您要么需要將您的reminder
對象轉換為這樣的類型(例如NSDictionary
),或者,這將是一個更清晰的解決方案,在reminder
對象的類中實現NSCoding
並使用NSKeyedArchiver
/ NSKeyedUnarchiver
對其進行NSKeyedUnarchiver
/解碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.