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

1

5

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)
Rahm answered 1/7, 2017 at 18:43 Comment(6)
perhaps you missed out on something while implementing it. Please put up some more code.Hosiery
thx for helping. I don't know how to get the identifier or the body content of the notification by calling the method didReceive response... Is there any way that I can send those data from the other ViewController to AppDelegate? More codes from ViewController below.Rahm
might look into this tut:- appcoda.com/local-notifications-ios8 It covers all the basic stuff.Hosiery
your settings for notification looks fine.. can you tell me again what do you want to do in the didReceive block?Hosiery
I want to get the content.body text or the identifier of the UNNotificationRequestRahm
In the link you gave me, it cannot edit or delete a specific item. For my app, I would like to edit or delete a specific, single item which requests the local notification.Rahm
L
11

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

Landgraviate answered 2/7, 2017 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.