我有一个奇怪的案例。我的 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/