在程序退到后台的时候马上弹出一个PUSH

 

- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/

NSLog(@"Application entered background state.");
// bgTask is instance variable
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
}];

dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = @"alertBd";
localNotif.alertAction = @"alertAtn";
localNotif.soundName = @"test.aif";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
break;
}
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
}

因为事件发生,弹出很多PUSH

在应用程序代理头文件中

UILocalNotification *localNotification;
NSTimer *timer;

在实现文件中:

- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
[self saveContext];


if (!localNotification) {
localNotification = [[UILocalNotification alloc] init];
}
old_count = 0;
if (!timer) {
timer = [[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(handle:) userInfo:nil repeats:YES] retain];
}

UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;

// Request permission to run in the background. Provide an

// expiration handler in case the task runs long.

// NSAssert(bgTask == UIBackgroundTaskInvalid, nil);
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case

// the task actually finishes at around the same time.

dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
// [app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];

// Start the long-running task and return immediately.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
// [app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});
}