我正在使用本地通知。我想删除已安排的通知。我不知道在哪里编写代码。这是我的代码..
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = self.selectedDate;
NSLog(@" self.selectedDate %@", self.selectedDate);
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"itemDate %@",itemDate);
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
NSLog(@"itemDate %@", localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [_titleTextFieldObj text];
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber =[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
NSLog(@" localNotif.applicationIconBadgeNumber ++ %ld", (long)localNotif.applicationIconBadgeNumber );
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[_titleTextFieldObj text] forKey:@"someKey"];
localNotif.userInfo = infoDict;
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
//UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
NSLog(@"notif %@",notificationArray);
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
我在这里写删除通知....
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
application.applicationIconBadgeNumber=1;
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSLog(@"userInfoCurrent %@",userInfoCurrent);
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
NSLog(@"uid %@",uid);
if ([uid isEqualToString:[notification.userInfo objectForKey:@"someKey"]])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}
if (notification) {
NSLog(@"notify %@",notification);
NSString *custom=[notification.userInfo objectForKey:@"someKey"];
NSLog(@"custom %@",custom);
NSString *newString = [custom stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"newString %@",newString);
NSLog(@"custmky%@",notification.description);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:newString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
alert.delegate=self;
[alert show];
}
}
我是UILocalNotifications和Objective-c的新手。任何人都可以帮助我....
答案 0 :(得分:0)
您可以通过将开火日期与已安排的UILocalNotifications或使用其正文进行比较来取消UILocationNotification。 以消防日期取消的一个例子:
UIApplication *application = [UIApplication sharedApplication];
NSDate *dateToCancel = nil; // Set this to the date you want to cancel
for (UILocalNotification *notification in [application scheduledLocalNotifications])
{
if (notification.fireDate == dateToCancel)
{
[application cancelLocalNotification:notification];
}
}
现在,如果您有通知指针,则只需调用取消本地通知,而无需循环已安排的通知。如果需要,还可以通过键对象方法向通知添加Id标记。
答案 1 :(得分:0)
无论代码在哪个类中,都可以使用UIApplication单例... 您可以使用以下方式取消所有通知:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
如果要删除特定通知,可以使用通知对象的userinfo,在创建本地通知时为其添加唯一ID。稍后您可以使用该ID删除本地通知。
为此您可以使用以下代码:
NSString *notificationId = @"id_to_cancel";
UILocalNotification *notification = nil;
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
if([notify.userInfo objectForKey:@"ID"] isEqualToString:notificationId ])
{
notification = notify;
break;
}
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];