Wasim Ahmad
Wasim Ahmad

Reputation: 71

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"]]; }

Upvotes: 1

Views: 3185

Answers (3)

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

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

Upvotes: 6

Simon
Simon

Reputation: 611

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.

Upvotes: 2

Weizhi
Weizhi

Reputation: 172

Try to check application.applicationState. If the app isn't active in the foreground , then not open URL.

Upvotes: 0

Related Questions