【问题标题】:Get Notification Body on iOS?在 iOS 上获取通知正文?
【发布时间】:2019-10-19 14:26:31
【问题描述】:

我想在单击操作按钮后获取通知的正文内容/文本并将该文本保存在变量中。

例如,我收到一条通知,并且有 2 个操作按钮。如果我单击其中的第一个,它将通知的正文内容保存在名为“内容”的变量中。如果我点击第二个,它将获得通知标题并将其保存在名为“Title”的变量中。

正文和标题内容是可变的。

实例:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    if response.actionIdentifier == "Yes" {
        //var Content = the content of the Notification
    } else {
        //var Title = the Title of the Notification
    }

    scheduleNotification()
    completionHandler()
}

【问题讨论】:

    标签: ios swift unusernotificationcenter


    【解决方案1】:

    保存到用户默认值

    设置数据:

     UserDefaults.standard.set("Title from notification", forKey: "TITLE")
    

    获取数据:

    let titleValue = UserDefaults.standard.value(forKey:"TITLE") as? String
    

    如果您想在使用后重置该值,只需保留同一键的 nil 值即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多