Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for alert action of UILocalNotification

UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [self.datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];

notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";

if the user clicks on "showme" the app should open and he should get the alert. Where should i write this code?and if possible someone please give me a little bit of code

like image 682
Chandu Avatar asked Nov 04 '11 11:11

Chandu


1 Answers

You will get the notification about the UILocalNotification in two places depending on app's state at the time the notification is fired.

1.In application:didFinishLaunchingWithOptions: method, if the app is neither running nor in the background.

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

    ...
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
    if (localNotif) {       
        // Show Alert Here
    }
    ...
}

2.In application:didReceiveLocalNotification: method if the app is either running or in background. Its almost useless to show the alert when the app is already running. So you have to show the alert only when the app was in background at the time the notification fired. To know if the app is resuming from background use the applicationWillEnterForeground: method.

- (void)applicationWillEnterForeground:(UIApplication *)application {

    isAppResumingFromBackground = YES;
}

Using this you can show the alert in didReceiveLocalNotification: method only when the app is resuming from background.

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

    if (isAppResumingFromBackground) {

        // Show Alert Here
    }
}

You can simply omit the if-condition if you want to show the alert view all the time the notification is fired regardless of the app's state.

like image 163
EmptyStack Avatar answered Oct 23 '22 23:10

EmptyStack