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

╄→гoц情女王★ 提交于 2020-01-24 11:43:47

问题


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)

回答1:


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



来源:https://stackoverflow.com/questions/44864265/get-local-notifications-body-text-or-identifier-in-appdelegate-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!