问题描述:

I might by fighting the windmills but I need to put some clearance into my knowledge of remote notifications. I want to handle notification while my app is turned off (killed from the app switcher) but user decides to turn it on not by tapping on notification but by tapping on app icon.

As far is I know, when app is killed and a remote notification arrives user has to tap on notification to pass it in launchOptions from didFinishLaunchingWithOptions: but... I want to handle this notification when user taps on app icon and nothing is passed to lauchOptions. In that scenario, at the moment I have no notification at all...

Is it possible to handle notification in background and perform action on it no matter how the app has been launched? Maybe didReceiveRemoteNotification: fetchCompletionHandler:?

网友答案:

Yeah, you are fighting the windmills :)

I recommend you to read Local and Push Notifications in depth. It's a bit long but the doc is clarifying.

In a fragment of this doc you can read:

If the application icon is tapped on a device running iOS, the application calls the same method (application:didFinishLaunchingWithOptions:), but furnishes no information about the notification.

And later on:

When handling remote notifications in application:didFinishLaunchingWithOptions: or applicationDidFinishLaunching:, the application delegate might perform a major additional task. Just after the application launches, the delegate should connect with its provider and fetch the waiting data. Listing 2-5 gives a schematic illustration of this procedure.

So having said this you could have a list of sent notifications for each device in your server and each time your app launches check if the device has any pending notification.

网友答案:

From the Local and Push Notification Programming Guide

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegates application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification

So, if your application is launched from the icon then you won't receive the notification data. You need your app to query the service/data store that is responsible for sending the notification to see if there is outstanding data/transactions/whatever.

相关阅读:
Top