2017-01-05 4 views
0

J'ai effectué une démonstration de base UILocalNotification. Lorsque Mon application passe en arrière-plan ou que mon simulateur est verrouillé, elle émet une notification. Mais ça ne marche pas quelqu'un peut-il me le dire?UILocalNotification ne fonctionne pas Lorsque l'application passe en arrière-plan ou que le simulateur est verrouillé

code

est

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

Je suis nouveau dans la nouvelle en ios, laissez-moi faire savoir si je ne me donne mistake.Or s'il vous plaît lien proposé pour répondre de façon précise. Merci Vous

+0

vous pouvez avoir une idée à travers ce http://stackoverflow.com/questions/40478287/how-to-maintain-timer-to-continue-running- quand-home-bouton-est-pressé/40479522 # 40479522 –

Répondre

1

Essayez cette ..

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