I want to access my application data in AppDelegate.swift whenever the app receives an action response. I was trying to use the
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "check" {
//do something to the app's data
}
completionHandler()
}
method, but I can't locate the data because I can't get the notification's identifier nor its body text. Could somebody help me on that? Thank you guys so much.
More Code:
//Setting content of the notification
let content = UNMutableNotificationContent()
content.title = "Scheduled Task"
content.body = taskDescriptionTextField.text!
content.badge = 1
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "alertCategory"
//Setting time for notification trigger
let date = datePicker.date
let dateCompenents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false)
//Adding Request
let request = UNNotificationRequest(identifier: taskDescriptionTextField.text!, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Do:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("original identifier was : \(response.notification.request.identifier)")
print("original body was : \(response.notification.request.content.body)")
print("Tapped in notification")
}
Basically what it is, is that you get a UNNotificationResponse
instance back. That object has two properties.
var actionIdentifier: String
var notification: UNNotification
<-- You need to use this one. Also a very very good tutorial for UNUserNotification framework can be found here
You only use the actionIdenfier
if you want to find out which action did the user choose when they were presented a notification (Did they just just tap on it?! Did they just dismiss it?! Or did they select on a customAction?!)
// From Apple documentation: the action identifier that the user can chose from:
* UNNotificationDismissActionIdentifier if the user dismissed the notification * UNNotificationDefaultActionIdentifier if the user opened the application from the notification * the identifier for a registered UNNotificationAction for other actions
What it means is, you can use to do something like:
switch actionIdentifier {
case UNNotificationDismissActionIdentifier: // Notification was dismissed by user
// Do something
completionHandler()
case UNNotificationDefaultActionIdentifier: // App was opened from notification
// Do something
completionHandler()
// Do something else
case customAction:
completionHandler()
default:
completionHandler()
}
To create custom actions you must:
For more information see this moment from WWDC 2016 Advance Notifications
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With