我的要求是阻止显示远程通知。
如果应用程序位于前台或后台,我可以通过以下方法处理此问题:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
//logic hide notification here
}
public func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//logic hide notification here
}
当应用程序被终止时,我尝试在 UNNotificationServiceExtension 中处理逻辑,但它不起作用:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
// logic hide notification here
}
我读过这篇文章:Is it possible to prevent a remote notification from being displayed? 据说苹果不允许这样做,这是正确的吗?
最佳答案
是的,这是正确的。
您无法捕获远程通知并将其隐藏。不接收远程通知的最佳方法是拒绝该权限。如果您的服务器发送远程通知,您可以将其配置为在您不需要时不发送通知。
发送通知而不显示通知的另一种方法是发送“静默通知”。它允许您接收通知、获取其内容并做您想做的事情(您只有 30 秒)。我认为,您可以发送静默通知、检查内容并在需要时发送本地通知,但我不确定。
关于ios - 当 iOS 应用程序被终止时,处理程序可以不显示远程通知吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59736987/