我使用本地通知。我想刪除已經安排notifications.I不知道在哪裏寫的code.Here是我的代碼..如何刪除應用程序中已經預定的LocalNotifications?
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。任何人都可以請幫我....
它是crashing.The錯誤是「試圖插入從對象[0]'零對象」。 – iworld 2014-12-08 07:13:53
這是因爲您需要使用ID創建本地通知。 – gronzzz 2014-12-08 12:11:21