HardikDabhi
HardikDabhi

Reputation: 2952

UILocalNotification is Not Working When app goes in background or simulator goes lock

I have Made one UILocalNotification Base demo. When My app goes background or my simulator goes lock it will fire notification. But It is not working can any one tell me?

Code is

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

    NSDate *todayDate = [[NSDate date]dateByAddingTimeInterval:5.0];
    UIApplication  *app = [UIApplication sharedApplication];
    UILocalNotification *notification = [[UILocalNotification alloc]init];
    if(notification)
    {
        notification.fireDate = todayDate;
        notification.timeZone  = [NSTimeZone defaultTimeZone];
        notification.repeatInterval = 0;
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.alertBody = @"Yor Are Working On Push Notification";
        [app scheduleLocalNotification:notification];

    }
}

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

    UIApplication *app  =[UIApplication sharedApplication];
    NSArray *oldNotification = [app scheduledLocalNotifications];
    if([oldNotification count ] >0)
    {
        [app cancelAllLocalNotifications];
    }
}

I am new in new in ios ,let me inform if i did mistake.Or please give me suggested link for specific answer. Thank You

Upvotes: 0

Views: 239

Answers (1)

Tapas Pal
Tapas Pal

Reputation: 7207

Try this..

- (void)applicationDidEnterBackground:(UIApplication *)application {
    UIBackgroundTaskIdentifier backgroundTaskIdentifire = [application beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifire];
    }];

    [self scheduleNotification];
}

- (void)scheduleNotification {
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"Title" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Yor Are Working On Push Notification" arguments:nil];

    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"notification" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
}

Upvotes: 1

Related Questions