我添加了UILocalNotification,重复间隔为一分钟。 怎么删除它?
答案 0 :(得分:1)
[[UIApplication sharedApplication] cancelAllLocalNotifications];
只需复制方法didLaunchWithOptions
或viewDidLoad
,甚至是下一个UILocalNotification
代码。
答案 1 :(得分:0)
- (BOOL)scheduleNotificationOn:(NSDate*) fireDate
text:(NSString*) alertText
action:(NSString*) alertAction
sound:(NSString*) soundfileName
launchImage:(NSString*) launchImage
andInfo:(NSDictionary*) userInfo
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
localNotification.soundName = soundfileName != nil ? soundfileName : UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = launchImage;
localNotification.userInfo = userInfo;
for (UILocalNotification *notification in [self allLocalNotifications]) {
if ([notification.fireDate timeIntervalSinceDate:localNotification.fireDate] == 0 &&
[notification.alertBody isEqualToString:localNotification.alertBody]
) {
return NO;
}
}
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
return YES;
}
- (NSArray *)allLocalNotifications
{
return [[UIApplication sharedApplication] scheduledLocalNotifications];
}
- (void)cancelLocalNotification:(UILocalNotification *)notification
{
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
- (void)cancelAllLocalNotifications
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
您应该创建一个唯一标识符来删除通知,例如alertBody .. 那么
- (void)cancelLocalNotificationByAlertBody:(NSString *)alertBody
{
for (UILocalNotification *notification in [self allLocalNotifications]) {
if ([notification.alertBody isEqual:alertBodyString] ) {
[self cancelLocalNotification:notification];
break;
}
}
}
答案 2 :(得分:0)
尝试
notification.repeatInterval = NSCalendarUnit(rawValue: 0)
此代码位于Swift中。在objective-c中尝试使用相同的东西。