gpt4 book ai didi

ios - 在远程通知上安排本地通知

转载 作者:行者123 更新时间:2023-12-01 21:59:12 24 4
gpt4 key购买 nike

在我们的项目中,我们想要更改 Remote Notification 的标题和正文。我们生成一个本地通知并显示一个更改了标题和正文的本地通知并隐藏推送通知。但是,当应用程序处于后台并终止状态时,它将显示远程通知而不是本地通知。但是我们想要显示一个本地通知而不是推送将呈现通知。这该怎么做 ?

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {

if notification.request.identifier != "local_notification1"{
self.isAdded = false
}
let name = (ContactsManager.shared.getContacName(withPhoneNumber:notification.request.content.title) != nil) ? ContactsManager.shared.getContacName(withPhoneNumber:notification.request.content.title) :
notification.request.content.title
let notificationContent = UNMutableNotificationContent()
// notificationContent.userInfo = notification.request.content.userInfo
notificationContent.body = notification.request.content.body
notificationContent.title = name!
debugPrint("name title is %@ ", name)
debugPrint("notificationContent title is %@ ", notificationContent.title)
notificationContent.sound = .default

let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let notificationRequest = UNNotificationRequest(identifier: "local_notification1", content: notificationContent, trigger: notificationTrigger)

if !isAdded {
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error {
debugPrint("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
}else {
print("is Shown")
}
self.isAdded = true
}
completionHandler([])

}
completionHandler([.alert,.sound])
}
}

最佳答案

您可以modify UNNotificationServiceExtension 远程通知的内容

  • 第一次覆盖 didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  • 修改内容和。
  • 返回 contentHandler 中的更新内容

  • 注:需要 iOS 10+

    关于ios - 在远程通知上安排本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60562417/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com