Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond to push notification view if app is already running in the background

I have something fairly simple I want to do. I attach a custom piece of data to some push notifications that I handle in

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

I look for the UIApplicationLaunchOptionsRemoteNotificationKey and hey presto there it is.

That method only gets called if my app is being launched for the first time. How do I read that same key if my application is running in the background already when the notification comes in and the user pressed the 'View' button on the notification? I want to send them to a particular view controller with that data open on it, the same as I do if the app is being launched for the first time from the notification.

like image 487
rustyshelf Avatar asked Feb 24 '11 01:02

rustyshelf


People also ask

Do push notifications work when app is open?

You have to put in the notification message title, icon, and content. You can send these messages using the Firebase console UI. In this way, a notification will be shown when the app is running in the background. Data Message: The app should handle these messages.

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

Check out application:didReceiveRemoteNotification:fetchCompletionHandler: in iOS 7 and later.


The method application:didReceiveRemoteNotification: is called if your app is running in the foreground. It also is called if your app is running in the background and the user engages with your push notification (thus making your app active).

So, the real question is how to determine if the app was in the foreground or if it was made active by the user engaging with your push notification.

It looks like this answer to the question didReceiveRemoteNotification when in background has the key:

You can tell whether your app was just brought to the foreground or not in application:didReceiveRemoteNotification: using this bit of code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {     if ( application.applicationState == UIApplicationStateActive )         // app was already in the foreground     else         // app was just brought from background to foreground     ... } 
like image 56
gerry3 Avatar answered Sep 21 '22 06:09

gerry3