当应用进入后台或模拟器锁定时,UILocalNotification 不起作用

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

我做了一个UILocalNotification Base demo。 当我的应用程序进入后台或我的模拟器进入锁定状态时,它将触发通知。 但是它不起作用谁能告诉我吗?

代码是

- (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];
    }
}

我是 ios 中的新人,如果我这样做了请告诉我 mistake.Or 请给我建议 link 以获得具体答案。 谢谢

试试这个..

- (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];
}