【问题标题】:Schedule Local notification on Remote Notification在远程通知上安排本地通知
【发布时间】:2020-03-06 10:44:16
【问题描述】:

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

【问题讨论】:

  • 我不认为Push Notification 可以静默触发Local Notification。用户可以点击Push Notification,然后添加可以触发Local Notification的代码。
  • 我的意思是说当 Push 发生时在 willPresent 方法中。我们想要显示本地通知而不是推送横幅。

标签: ios swift push-notification notifications local


【解决方案1】:

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

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

  2. 修改内容和。

  3. 返回contentHandler中的更新内容

注意:需要 iOS 10+

【讨论】:

  • 我不想使用服务扩展。我想在收到推送通知时显示本地通知。因为我有与用户默认相关的功能,这就是我的应用程序在服务扩展中无法访问的原因。
  • 没有Service Extension是不可能的
  • 可以在Service Extension中访问userdefault
  • 您可以使用静默通知!但我认为它不可靠,因为如果应用程序被终止,通知可能根本不会发送
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
相关资源
最近更新 更多