本地和推送通知

问题描述 投票:0回答:2

在我们的项目中,我们想更改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])
   }    
}
ios swift push-notification notifications local
2个回答
0
投票

有两种实现方法,每种方法都有自己的权衡:

  1. Silent notifications不会向用户显示任何内容,因此您无需隐藏任何内容。但是它们的优先级较低,因此到达时间不可靠。

  2. PushKit notifications即使被杀死也唤醒您的应用。但是只允许将它们用于特定目的:更新Apple Watch,响应文件提供者的更改以及接收VoIP呼叫。

我认为最好的选择可能是3:首先从服务器发送正确的通知数据...


0
投票

您可以在modify的帮助下UNNotificationServiceExtension远程通知的内容

  1. 第一优先UNNotificationServiceExtension

  2. 修改内容和。

  3. 返回didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {中的更新内容

注意:必需的iOS 10 +

© www.soinside.com 2019 - 2024. All rights reserved.