我有一个问题如下:
我的问题是本地通知仍处于活动状态,并且在将其从后台移除后仍然每5分钟弹出一次。
我怎么能阻止它? 请帮我! 谢谢你提前。
答案 0 :(得分:2)
将它放在应用程序委托中。当应用程序进入后台时,它将删除所有本地通知。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
答案 1 :(得分:1)
如果您不想取消所有通知...我已设置存储在通知的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中解除了。希望这有帮助