繁体   English   中英

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

[英]Schedule Local notification on Remote Notification

在我们的项目中,我们想要更改 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])
   }    
}

您可以借助UNNotificationServiceExtension 修改远程通知的UNNotificationServiceExtension

  1. 首先覆盖didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

  2. 修改内容和。

  3. contentHandler返回更新的内容

注意:需要 iOS 10+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM