【问题标题】:Get local notification's body text or identifier in AppDelegate Swift在 AppDelegate Swift 中获取本地通知的正文或标识符
【发布时间】:2017-12-05 11:09:19
【问题描述】:

每当应用收到操作响应时,我想在 AppDelegate.swift 中访问我的应用数据。我试图使用

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == "check" {
        //do something to the app's data
    }

    completionHandler()

}

方法,但我无法找到数据,因为我无法获取通知的标识符或其正文。有人可以帮我吗?非常感谢你们。

更多代码:

//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)

【问题讨论】:

  • 也许你在实现它时错过了一些东西。请提供更多代码。
  • 感谢您的帮助。我不知道如何通过调用方法 didReceive 响应来获取通知的标识符或正文内容...有什么方法可以将这些数据从其他 ViewController 发送到 AppDelegate?下面来自 ViewController 的更多代码。
  • 可能会看看这个 tut:- appcoda.com/local-notifications-ios8 它涵盖了所有基本的东西。
  • 你的通知设置看起来不错..你能再告诉我你想在 didReceive 块中做什么吗?
  • 我要获取content.body文本或者​​UNNotificationRequest的标识符

标签: ios swift swift3 unnotificationrequest


【解决方案1】:

做:

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")
}

基本上它是你得到一个UNNotificationResponse 实例。该对象有两个属性。

  • var actionIdentifier: String
  • var notification: UNNotification

还有一个非常好的UNUserNotification框架教程可以找到here


如果您想了解用户在收到通知时选择了哪个操作(他们是否只是点击它?!他们是否只是关闭它?!或者他们是否选择了customAction?!)

// 来自 Apple 文档:用户可以从中选择的操作标识符:

* 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

意思是,你可以用来做类似的事情:

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()
}

要创建自定义操作,您必须:

  • 在类别中创建操作
  • 注册类别

欲了解更多信息,请查看WWDC 2016 Advance Notifications的这一刻

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    相关资源
    最近更新 更多