【发布时间】: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