以下是我的一个问题:
我的问题是,本地通知仍然处于活动状态,并且在从背景中删除后每5分钟仍会弹出一次。
我怎么才能阻止它?请帮帮我!谢谢你的进阶。
发布于 2012-07-23 07:34:13
将其放入应用程序委托中。当应用程序进入后台时,它将删除所有本地通知。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}发布于 2012-07-23 08:48:30
如果你不想取消所有通知..。我已经设置了一个存储在通知的userInfo字典中的唯一标识符。当我想要删除时,我会快速枚举所有通知,并选择正确的通知进行删除。
这里的绊脚石是记得存储我为通知创建的UUID,还记得在快速枚举中使用isEqualToString。我想我也可以使用一个特定的名称字符串,而不是唯一的标识符。如果有人能让我知道一个比快速枚举更好的方法,请告诉我。
@interface myApp () {
NSString *storedUUIDString;
}
- (void)viewDidLoad {
// create a unique identifier - place this anywhere but don't forget it! You need it to identify the local notification later
storedUUIDString = [self createUUID]; // see method lower down
}
// Create the local notification
- (void)createLocalNotification {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
localNotif.fireDate = [self.timerPrototype fireDate];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Hello world";
localNotif.alertAction = @"View"; // Set the action button
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:storedUUIDString forKey:@"UUID"];
localNotif.userInfo = infoDict;
// Schedule the notification and start the timer
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
// Delete the specific local notification
- (void) deleteLocalNotification {
// Fast enumerate to pick out the local notification with the correct UUID
for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[localNotification.userInfo valueForKey:@"UUID"] isEqualToString: storedUUIDString]) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
}
// Create a unique identifier to allow the local notification to be identified
- (NSString *)createUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (__bridge NSString *)string;
}在过去6个月的某个时候,上述的大部分可能已经从StackOverflow上取消了。希望这能有所帮助
https://stackoverflow.com/questions/11608087
复制相似问题