didReceiveRemoteNotification 不是 运行 当应用程序是 运行(前景)

didReceiveRemoteNotification is not running when app is running (FOREGROUND)

我有一个奇怪的案例。我的 swift ios 应用已连接到 Cloudkit。如果应用程序不是 运行ning(后台状态),我每次都会收到我的通知徽章和警报! 如果应用是 运行ning,则不会收到任何通知!我知道它没有击中遥控器,因为我这样做: 1.在didReceiveRemoteNotification事件上加断点 2. 运行 xcode 插入 iphone 3. NSLog("detected didReceiveRemoteNotification"),所以最终代码是这样的

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
  NSLog("detected didReceiveRemoteNotification")
}

我知道错误不是来自 cloudkit 或来自 APNS,因为当 phone 处于后台状态时我确实收到了警报横幅和徽章。

能否指导我正确设置前景状态!?

我是运行ning ios v9.3

更新 #1 我认为文档的措辞很差。它清楚地表明前景中的 运行 都是我关心的;尽管如此,修复比文档更准确!

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background.

你用错了方法。实施:

func application(_ application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)

如文档所述:

Unlike the application:didReceiveRemoteNotification: method, ... the system calls this method when your app is running in the foreground or background.