didReceiveRemoteNotification is not running when app is running (FOREGROUND)

爷,独闯天下 提交于 2019-11-30 09:55:48

问题


I have a strange case. My swift ios app is connected to Cloudkit. If the app is NOT running (background state), I receive my notifications badge and alert just fine, every time! If the app is running, no notifications are received! I know it is not hitting the remote because I do this: 1. Adding a breakpoint to the didReceiveRemoteNotification event 2. Running xcode in a plugged iphone 3. NSLog("detected didReceiveRemoteNotification"), so final code look like this

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

I know the error is not coming from cloudkit or from APNS because I do receive alert banner and badge when the phone is in the background state.

Can you guide me to set this up properly for the Foreground state!?

I am running ios v9.3

UPDATE #1 I think the wording of the documentation is poor. It clearly says that both run on the foreground, which is what I cared about; nevertheless, the fix is more accurate than the documentation!

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.


回答1:


You've implemented the wrong method. Implement:

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

As the docs explain:

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



来源:https://stackoverflow.com/questions/37232849/didreceiveremotenotification-is-not-running-when-app-is-running-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!