更新时间:2023-02-26 21:00:49
经过互联网搜索后,我得到的一件事我们不能编辑
a UILocal通知一旦添加。但确定我找到了另一种方式。
After searching a lot over internet, I got a thing that we can't edit
a UILocal Notification once added. But sure there is another way that I have found.
以下是添加通知的方法。
-(void)setLocalNotification
{
UILocalNotification *eventLocalNotification = [[UILocalNotification alloc]init];
eventLocalNotification.fireDate = //set firing Date of NSDate type
eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
eventLocalNotification.alertBody = [NSString stringWithFormat:@"An event has arrived\n Event Name: %@",notificationName.Text];
eventLocalNotification.soundName = UILocalNotificationDefaultSoundName;
if ([repeat isEqualToString:@"Once"]){
eventLocalNotification.repeatInterval = 0;
}else if ([repeat isEqualToString:@"Daily"]){
eventLocalNotification.repeatInterval = NSDayCalendarUnit;
}else if ([repeat isEqualToString:@"Weekly"]){
eventLocalNotification.repeatInterval = NSWeekCalendarUnit;
}else if ([repeat isEqualToString:@"Monthly"]){
eventLocalNotification.repeatInterval = NSMonthCalendarUnit;
}else if ([repeat isEqualToString:@"Yearly"]){
eventLocalNotification.repeatInterval = NSYearCalendarUnit;
}
NSDictionary *info = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@",notificationName.text] forKey:@"name"];
eventLocalNotification.userInfo = info;
NSLog(@"notification userInfo gets name : %@",[info objectForKey:@"name"]);
[[UIApplication sharedApplication] scheduleLocalNotification:eventLocalNotification];
NSLog(@"Notification created");
}
以下是取消通知的功能
-(void)cancelLocalNotification
{
UILocalNotification * notificationToCancel=nil;
for(UILocalNotification * aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
NSLog(@"%@",[aNotif.userInfo objectForKey:@"name"]);
if([[aNotif.userInfo objectForKey:@"name"] isEqualToString:notificationName ])
{
notificationToCancel = aNotif;
[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
NSLog(@"Notification Cancelled");
break;
}
}
}
希望你会从中受益。运气***
Hope you will get benefit from it. Best of Luck