Armen Yeghiazaryan
Armen Yeghiazaryan

Reputation: 59

Is it possible to get Local Notification when application's state is inactive

I'm developing an iOS application, and I have to get local notification, BUT in case of application's state is inactive. I successfully get notifications when application is in background state. So, is it possible to get local notification, when application is inactive? Or maybe that's possible only by using push notification?

Regards, Armen

Upvotes: 1

Views: 5565

Answers (3)

Petr Syrov
Petr Syrov

Reputation: 15313

Sure, just use willResignActiveNotification notification listener as listed here

Upvotes: 0

pkc
pkc

Reputation: 8516

- (void)sendNotification
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//    notification.repeatInterval = NSDayCalendarUnit;
    localNotification.fireDate = vwPicker.date;
    localNotification.alertBody = txtAlarmTitle.text;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.userInfo = @{@"Title": txtAlarmTitle.text};
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [self handleNotification:notification application:application];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification)
        [self handleNotification:localNotification application:application];

    return YES;
}

-(void)handleNotification: (UILocalNotification *)notification application:(UIApplication *)application
{
    NSString *title = [notification.userInfo objectForKey:@"Title"];

    [[[UIAlertView alloc]initWithTitle:@"Smart Alarm" message:title delegate:self cancelButtonTitle:@"Answer the Teaser" otherButtonTitles: nil] show];

    application.applicationIconBadgeNumber = 0;
}

Upvotes: 2

Robin Summerhill
Robin Summerhill

Reputation: 13675

You need to respond to local notifications in two places in your app delegate:

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

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

The first is for when your app was not running - use the launchOptions parameter to check if your app was launched due to a local notification.

The second is for when your app is currently running (active or inactive). You can check if the app is inactive by checking NSApplication's applicationState property in the application:didReceiveLocalNotification: method.

Upvotes: 9

Related Questions