Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle push notification when app is in background but not suspended

My app receiving push notification, and showing appropriate info message for that. However when I'm clicking to the message, application becomes active but application didFinishLaunchingWithOptions is not getting called which is right i think, since the application is not suspended and it just resigns active. The question is how i can make sure that user clicked to message when application becomes to foreground ?

like image 378
taffarel Avatar asked Jun 16 '15 13:06

taffarel


People also ask

Can you send local notifications while app is in background?

Notifications could be created at any moment (on Foreground, Background or even when the application is terminated/killed).

How do I handle background notifications on Android?

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.


1 Answers

I think what you are looking for is this app delegate method:

- (void)application:(UIApplication *)application     
       didReceiveRemoteNotification:(NSDictionary *)userInfo

It will be called if your app is backgrounded, and the notification payload will be delivered in the userInfo dictionary. This contrasts with the situation when the app is launched from cold start, when this method does not get called, and instead you check in the launchOptions dictionary for the payload.

However the preferred way to do this since iOS7 is to use this:

- (void)application:(UIApplication *)application 
       didReceiveRemoteNotification:(NSDictionary *)userInfo 
             fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;

This method is called when a user taps on a notification, regardless of whether the app is launched from cold start or foregrounded from background. So even if you are not using the completionHandler, it provides a more consistent way of accessing the notification payload. If this method is present, the older one does not get called.

like image 115
foundry Avatar answered Oct 04 '22 09:10

foundry