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 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
iOS push notification: how to detect if the user tapped on notification when the app is in background?
OK I finally figured out.
In the target settings ➝ Capabilities tab ➝ Background Modes, if you check "Remote Notifications", application:didReceiveRemoteNotification:
will get triggered as soon as notification arrives (as long as the app is in the background), and in that case there is no way to tell whether the user will tap on the notification.
If you uncheck that box, application:didReceiveRemoteNotification:
will be triggered only when you tap on the notification.
It's a little strange that checking this box will change how one of the app delegate methods behaves. It would be nicer if that box is checked, Apple uses two different delegate methods for notification receive and notification tap. I think most of the developers always want to know if a notification is tapped on or not.
Hopefully this will be helpful for anyone else who run into this issue. Apple also didn't document it clearly here so it took me a while to figure out.
Get push notification while App in foreground iOS
If the application is running in the foreground, iOS won't show a notification banner/alert. That's by design. But we can achieve it by using UILocalNotification
as follows
Check whether application is in active state on receiving a remote
notification. If in active state fire a UILocalNotification.if (application.applicationState == UIApplicationStateActive ) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = message;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
SWIFT:
if application.applicationState == .active {
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
}
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.
Related Topics
How to Remove Border from Segmented Control
What Is Difference Between Urlwithstring and Fileurlwithpath of Nsurl
How to Properly Group a List Fetched from Coredata by Date
Nsoperationqueue Serial Fifo Queue
Corebluetooth: What Is the Lifetime of Unique Uuids
Ckquery from Private Zone Returns Only First 100 Ckrecords from in Cloudkit
Apple Llvm 6.0 Error: Clang Failed with Exit Code -1
iPhone Storage in Tmp Directory
Simple Low-Latency Audio Playback in iOS Swift
Invalid Image Path - No Image Found at the Path. Cfbundleicons Xcode 5
How to Execute Some Code After a Segue Is Done
Switui - Two Navigationlink in a List
Setting Multiple Times for Notifications in Swift
Add Modal Popup to Map Annotation - Swift 3.0
Use Different Googleservice-Info.Plist for Different Build Schemes
Application(...Continue Useractivity...) Method Not Called in iOS 13