swift - 应用程序运行时 didReceiveRemoteNotification 未运行(前台)

标签 swift apple-push-notifications ios9

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

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

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

你能指导我为前台状态正确设置它吗!?

我运行的是 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.

关于swift - 应用程序运行时 didReceiveRemoteNotification 未运行(前台),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595509/

相关文章:

ios - 更新推送通知的配置文件

objective-c - iOS9 - UITableViewCellContentView 覆盖了 Cell 中的控件

ios - UIButton 不会调用@IBAction

ios - Realm 迁移一对多

ios - 从 AppDelegate 中的 NSMutableDictionary 清除几个推送通知

ios - UNNotificationRequest 仅在 iPhone 解锁时播放自定义声音

ios - 在 iOS 9 中禁用网络时,HTTP GET 请求出现 fatal error

ios - 有没有办法在 SFSafariViewController 发出的初始请求中设置 referer?

ios - 当我移动触摸点时,我如何才能知道我是否有一个 UIView 长按?

iOS - 在多个线程上处理照片