静默推送通知仍会正常弹出

Silent push notifications still pop up as normal

我正在尝试设置静默推送通知,但遇到了这个问题。 JSON 我发送给 APNs 的是:

{
  "aps": {
    "alert": "test",
    "badge": 0,
    "content-available": 1
  }
}

委托方法是:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    var pushData = userInfo["aps"] as? [AnyHashable : Any];
    if pushData?["content-available"] as? Int == 1 {
        NSLog("received silent notification")
        completionHandler(.noData)
    } else {
        NSLog("received notification")
        completionHandler(.newData)
    }
}

当后端发送推送通知时,我的应用程序处于后台。 XCode 显示 'received silent notification',但此通知仍正常弹出。你能告诉我,我在做什么错了吗?它可能不应该发生,对吧?

项目设置 'remote notifications' 在 'background modes' 中签入。

静默推送通知的正确 json 负载应如下所示

{
    "aps" = {
        "content-available" : 1,
        "sound" : ""
    };
    // add custom key-value pairs 
}