How to determine that app become active by tapping on local notification - ios

An app is running on iPhone and user tap the home button once and app will enter background.
after 2 or 3 seconds local notification arrives and user tap on local notification.
app will again enter in foreground and become active and didReceiveLocalNotification will be called.
How to determine that app become active by tapping on local notification not the app icon.

Here is an easy way to detect what's your App's status when UILocalNotification fired and if
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
is called, this makes sure that local notification is received.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
} else {
}
}

Related

How to handle remote notification with background mode enabled

I build and app that has Background Modes enabled, and the push notification payload that the app gets has "content-available" key.
This setup results in didReceiveRemoteNotification being called EVERY TIME the app gets a push notification, which means that if i get 3 push notifications while the app is in the background - the function will fire 3 times and the code inside it will be executed when the app will applicationDidBecomeActive
My biggest problem is that there is NO way to know if a user tapped the Push System Alert or tapped the app icon to bring the app from background, since regardless of the user's action, the didReceiveRemoteNotification will fire.
Is there a way to know for sure that the user tapped on the Sys alert?
and this: http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/
and other answers
don't seem to be helpful
For app is background push
- (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
}
}
For app is terminate state
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil) {
// Launched from push notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
}
}

iOS Push Notifications Flow

I'm working on push notification flow but not getting exactly how to handle it. I need a simple explanation, when push notification comes, which delegate is called
When user tap on push notification label
When user tap on App icon when push notification comes
I'm unable to maintain to my app application state, for me the flow should be like:
When user tap on push notification label: It should open a particular viewcontroller
When user tap on App icon when push notification comes: It should open same viewcontroller from where app goes in background
How I can achieve this in Xcode 8.1/iOS 10.1.1?
Also I'm using background mode remote notification and background fetch.
In AppDelegate.m used it to check where user tap icon
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Handle notification when the user click it while app is running in background or foreground.
if(application.applicationState == UIApplicationStateInactive) {
NSLog(#"Inactive - the user has tapped in the notification when app was closed or in background");
//do some tasks
}
else if (application.applicationState == UIApplicationStateBackground) {
NSLog(#"application Background - notification has arrived when app was in background");
}
else {
NSLog(#"application Active - notication has arrived while app was opened");
//do tasks
}
}
When a push notification sends from server a delegate call on app side but you have to register from app end by adding registerForPushNotifications.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//Handle notification when the user click it while app is running in background or foreground.
//Where userinfo is a dict. It has the data sent from server
}

how to detect user clicked iOS remote notification?

When user taps on a remote notification, the following callback is triggered in the app delegate:
-application:didReceiveRemoteNotification:fetchCompletionHandler:
in this scenario, application is launched and the app state is UIApplicationStateActive which I interpret it as user actioned on a remote notification.
the problem:
This method can also get called when a remote notification arrives and app is in the foreground with inactive state.
example: when notification center view is open(swipe from top edge of screen down) or a UIAlert is open. In both case application state is UIApplicationStateActive and there is no way to tell whether it's a user actioned notification or system push received.
Q: How can I determine if didReceiveRemoteNotification callback is response to user tapping on remote notification vs arrival of remote notification?
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
//When your app is in foreground and it received a push notification
}
else if (state == UIApplicationStateInactive)
{
//When your app was in background and it received a push notification
}
Also, didFinishLaunchingWithOptions will be called if the app was not running and the user tapped a notification. I have not tried it but i can assume you can get the notification details from options.
To differentiate both calls in didReceiveRemoteNotification you can add this code from below.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:#“inactivePush" object:nil];
}
else if([UIApplication sharedApplication].applicationState==UIApplicationStateActive){
[[NSNotificationCenter defaultCenter] postNotificationName:#"appOpenPush" object:nil];
}
//When the app is in the background
else {
}//End background
}
}

Tap on local notification iOS

First I want to know, which specific method is called when i tap on local notification. I want to open a url upon tap on notification. below is code app delegate.
Now the issue is, url opens automatically even if i don't tap on notification. Please guide me if u know that. Thank you
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notifyAlarm
{
application.applicationIconBadgeNumber = 0;
NSLog(#"Notification tapped :) ");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com.pk"]]; }
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notifyAlarm
This methos is called every time when notification fire.
To open url when tap on notification you have to check state of the app.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
appState = application.applicationState;
if (appState == UIApplicationStateActive)
{
// Don't open Url.
}
else
{
// Open Url.
}
}
Have a look at the Local and Remote Notification Programming Guide from Apple, section "Handling Local and Remote Notifications".
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
Later is says
[...] get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.
Try to check application.applicationState. If the app isn't active in the foreground , then not open URL.

Find event launching the application: banner notification, alert notification or icon tap?

Can I find out which event launching the application on iOS ?
I need to distinguish between the followings:
Icon tap
Banner notification
Alert notification
Alternatively, can I view the application local settings (set for Banner or alert notification)?
To determine if the app was launched from the using pressing a notification, you need to implement 2 methods.
First, in application didFinishLaunchingWithOptions, do something like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
// Handle notification
}
Second:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive) {
// The app was open when a remote notification was received...
} else {
// The app was in the background and just came to the foreground in response to the user pressing the push notification
}
}
You can view which notification types have been enabled using:
UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
However, you can't distinguish between Banner or Alert notifications.

Resources