APNS 静默远程通知未触发 - SWIFT 3

APNS Silent Remote Notification not triggering - SWIFT 3

场景:

1) 应用程序在前台时的静默或正常负载:

2) 应用程序在后台时静默负载:

3) 应用程序在后台时的正常负载:

  1. 如果用户点击通知打开应用程序。

    • 触发 application:didReceiveRemoteNotification:fetchCompletionHandler
  2. 如果用户点击应用程序图标打开应用程序:

    • 什么都没发生

这些是我用于 APN 的有效载荷:

正常负载:

{  
  "aps":{  
    "alert":"andre test",
    "badge":0,
    "sound":"default",
    "content-available":1
  },
  "acme-syncalarm":"true"
} 

静默负载:

{  
  "aps":{  
    "content-available":1
  },
  "acme-syncalarm":"true"
}

我已经使用以下代码实现了远程推送通知:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    print("Receeeeeeeeived: \(userInfo)")
    UIApplication.shared.applicationIconBadgeNumber = 11

    completionHandler(.newData)
}

我还实现了这个来检查应用程序是否正在从终止状态中恢复(正如我在一些问题中读到的那样),但是代码从未进入 print(rn) 行。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if let rn = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
            print(rn)
        }
 }

编辑 1 - 我还为应用程序在后台模式下启用了远程通知。

我需要做什么才能涵盖 "Nothing Happens" 场景? 1、2 和 3.2 ?

一些注意事项:

  • "If user open the App clicking the App icon: Nothing happens" <-- 这是预料之中的,因为您没有直接与任何通知交互。想象一下,如果您收到了 5 条通知。您如何知道应该处理哪个通知...

  • 普通负载不会有任何名为 content-available 的密钥。所以这又是一个无声通知。能不能先看看我的回答?

一些建议:

  • 确保您已在后台模式下启用远程通知。像这样:

  • 另见here。 iOS 11 个初始版本存在静默通知问题。确保您有最新版本的测试,否则它将无法工作。如果您有 iOS 10 设备,那么首先尝试使用该设备进行测试...

  • 确保您的设备上有后台应用程序刷新和通知可用。要查看如何操作,请参阅我的链接答案。

  • 您是自己创建负载还是使用 FireBase?如果您使用的是 Firebase,那么某些键会发生变化……您必须相应地进行调整。

  • 确保您已将某些对象设置为 UNUserNotificationCenterDelegatedelegate 例如:

UNUserNotificationCenter.current().delegate = delegateObject

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let content = notification.request.content
    // Process notification content

    completionHandler([.alert, .sound, .badge]) // Display notification as regular alert and play sound
}

复制自 here 的代码。

如果您不这样做,那么当应用程序位于前台时,您将不会显示任何通知。这应该可以解决当应用程序处于前台并且您收到 正常 远程通知时的问题。