0

I want to fire local notification on particular time daily, i have wrote this code snippet but it is not receiving notification.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 //  running on iOS8
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else // iOS 7 or earlier
    {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }

}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"didReceiveLocalNotification----");
    application.applicationIconBadgeNumber = 0;

}

//ViewController class

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

    [dateComponents setHour:7];
    [dateComponents setMinute:59];
    NSDate *currentDate = [NSDate date]; //2015-04-13 07:56:09 +0000
    NSDate *fireDate = nil;
    fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
                                          toDate:currentDate
                                         options:0];

    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = @"Notiication success....";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;




    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

My Log shows didReceiveLocalNotification but notification not appearing in device :(

Where i am making mistake please help.

Thanks in advance

Krunal
  • 6,440
  • 21
  • 91
  • 155

1 Answers1

0

The notification won't appear while app is in foreground. The notification will show up only if the app totally close or in the background.

Asaf
  • 2,158
  • 2
  • 25
  • 40