How to Handle Push Notifications If the Application Is Already Running

How to handle push notifications if the application is already running?

You can implement application:didReceiveRemoteNotification:

Here is a possible sample code:

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:@"alert"];
if ([alert isKindOfClass:[NSString class]]) {
message = alert;
} else if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:@"body"];
}
if (alert) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"AThe message." delegate:self
cancelButtonTitle:@"button 1"
otherButtonTitles:@"button", nil];
[alertView show];
[alertView release];
}

Push Notification when app is running

Using the activity manager doesn't exactly work as expected. The activity manager keeps a track of all the running apps on the phone. It doesn't really tell you whether the app is in foreground or background. To check whether the activity is running, set a boolean value in the onResume and onPause method of the activity.

Example:

  public void onResume()
{
super.onResume();
isActivityRunning = true;
}
public void onPause()
{
super.onPause();
isActivityRunning = false;
}

You can then use the isActivityRunning to see if you want to throw the notification or not.

Also see this: Checking if an Android application is running in the background

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

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
...
}

How can you get the payload of a push notification if app is already running in the background when the user taps the notification?

"because the app was in the background when the push arrived didReceiveRemoteNotification: won't have been called."

This, or didReceiveRemoteNotification:withExpirationHandler, should get called if the app is in the background and gets switched to the foreground when the user taps on the notification.

However I got into a situation when this wasn't working as the reason was the content of the push was incorrect, I can't remember the details but double check what's in there.

Handling Push Notification While App is Open

I would recommend checking the applicationState property in UIApplication to determine if the app is running in the background or not.

Starting app only if its not currently running

Use a "launch Intent" for your app, like this:

PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("your.package.name");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0);

Replace "your.package.name" with the name of your package from the Android manifest.

Also, you should remove the special launchMode="singleTask" from your manifest. Standard Android behaviour will do what you want.

Detect if the app was launched/opened from a push notification

See This code :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//opened from a push notification when the app was on background
}
}

same as

-(void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification


Related Topics



Leave a reply



Submit