Apple Push Notification setting up Remote Notifications method overrides other methods

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have properly set up my app for Remote Notifications with the method

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

and it processes it correctly now every time the app receives an Apple Push Notification.

But my issue is that this method is being called in all instances now.

didFinishLaunchingWithOptions does not seem to be called anymore? Or the launchOptions is coming up empty?

Basically I used the didReceiveRemoteNotification as a catch all to process any incoming notifications which is my intended purpose, but then when I click the notification itself, it fires the didReceiveRemoteNotification again. Thus processing the notification twice, which I do not want.

So this is how I want my app to handle notifications:

  • Anytime a notification is received, would like to automatically process the notification in the background
  • When user clicks notification from outside app, to run a different method than the didReceiveRemoteNotification
  • When user is inside app, to run a different method than the other

回答1:

Apple does not allow what you want. Push notifications work in the following way

1) When the application is not running, and the user clicks a notification, the application is launched and the payload of the notification is loaded in the function didFinishLaunchingWithOptions.

2) When the application is running in background, and the user clicks a notification, the application becomes active and the function didReceiveRemoteNotification is called. Now this function contains the payload of the notification.

3) When the application is running and a push notification is called, the function didReceiveRemoteNotification is called and this function contains the payload of the notification.

4) When the app is running in background or inactive and notification is received, nothing can be done with the notifications until the user clicks the notification



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