Question

I am using Repeat Local notofications to display alerts to the user. For this i used below code

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
localNotification.alertBody = @"sss";
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Using NSNotification Centre i will call the local notification every 60 seconds. It is working fine.

Also in Appdelegate i use the following code:-

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
        NSLog(@"Received");
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateActive) {

             [[NSNotificationCenter defaultCenter] postNotificationName:@"RestartProcess" object:self];

}
 application.applicationIconBadgeNumber = 0;
}

Also

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (locationNotification) {
        // Set icon badge number to zero
        NSLog(@"Yes Notid=fications is predsent");
        application.applicationIconBadgeNumber = 0;
    }

    return YES;
}

My problem is when local notification starts, i pressed on home button then waited to display. After the time period display a notification message. When i clicked on app icon the process is stopped.After that Local notifications not working.But when i was clicked on Notification Message it will works right.How i can fix the problem.Can any one help me to do this. Thanks in advance..

Was it helpful?

Solution

didReceiveLocalNotification is only called when you hit on notification messaage. If you want to do it in applicationDidBecomeActive you have to do it manually.

You can check badge number count(if notification consists badge) like :

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

    if(application.applicationIconBadgeNumber >= 1)
    {
       //Do you stuff here
        application.applicationIconBadgeNumber = 0;

    }
}

OTHER TIPS

When you press home button:

UIApplicationState state = [application applicationState];
// at this time, state == UIApplicationStateInactive

When you click your application, i think you should implement in this method:

- (void)applicationDidBecomeActive:(UIApplication *)application- (void)applicationWillEnterForeground:(UIApplication *)application
{
 UIApplicationState state = [application applicationState];
 application.applicationIconBadgeNumber = 0;
 //state at this time is UIApplicationStateInactive
 if (state == UIApplicationStateInactive) {   
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RestartProcess" object:self];
 }
}

Hope this helps you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top