当 iOS 应用程序被杀死时,处理程序可以不显示远程通知吗?

Can handler not show remote notification when iOS app is killed?

我的需求是不显示远程通知。
如果应用程序处于前台或后台,我可以通过以下方法处理此问题:

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? 上面说苹果不允许这样做,对吗?

是的,没错。

您无法捕获远程通知并将其隐藏。不接收远程通知的最好方法是拒绝许可。如果您的服务器发送远程通知,您可以将其配置为在不需要时不发送通知。

另一种发送通知但不显示通知的方法是发送 "silent notification"。它允许您接收通知、获取其内容并执行您想要的操作(您只有 30 秒)。我认为,您可以发送静默通知、检查内容并在需要时发送本地通知,但我不确定。

您是否正在尝试完全删除通知? (前景和背景?) 如果是这种情况,那么您应该要求您的后端服务向您发送静默通知而不是普通通知。你不应该自己处理。