Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get local notification's body text or identifier in AppDelegate Swift

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)
like image 516
Eric Hua Avatar asked Jul 01 '17 18:07

Eric Hua


1 Answers

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:

  • create an action inside a category
  • register the category

For more information see this moment from WWDC 2016 Advance Notifications

like image 128
mfaani Avatar answered Oct 23 '22 20:10

mfaani