更新本地通知的日期,并取消以前的通知 [英] Update fire date for local notification and cancel previous notification
问题描述
其实我想要的是先前的通知必须被取消,即日期必须修改为编辑的,通知应该在新的日期设置和触发。 / p>
这是我如何设置通知,示例代码:
(void)setNotification
{
//确认保存数据后设置通知
类cls = NSClassFromString(@UILocalNotification);
reminderNotification = [[cls alloc] init];
if(cls!= nil)
{
NSDateFormatter * dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@YYYY-MM-dd HH:mm:ss];
NSDate * notificationDate = [dateFormat dateFromString:textField2.text];
reminderNotification.fireDate = notificationDate;
reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
NSString * reminderText = [NSString stringWithFormat:@%@'%@ on%@,textField.text,textField1.text,strDate];
reminderNotification.alertBody = reminderText;
reminderNotification.alertAction = @View;
reminderNotification.soundName = @lazy_afternoon.mp3;
reminderNotification.applicationIconBadgeNumber = 1;
NSDictionary * userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder];
reminderNotification.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification];
[reminderNotification release];
}
}
任何一个请指导我正确的路径感谢所有提前:)
p> 使用此方法计划通知,其中notificationID必须是唯一的
- (void )scheduleNotificationForDate(NSDate *)date AlertBody:(NSString *)alertBody ActionButtonTitle:(NSString *)actionButtonTitle NotificationID:(NSString *)notificationID {
UILocalNotification * localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.alertBody = alertBody;
localNotification.alertAction = actionButtonTitle;
localNotification.soundName = @yourSound.wav;
NSDictionary * infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
使用此方法取消使用该通知标识的特定通知
- (void)cancelLocalNotification:(NSString *)notificationID {
//循环所有排定的通知和取消我们正在寻找的
UILocalNotification * cancelThisNotification = nil;
BOOL hasNotification = NO; $ {
(UILocalNotification * someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]){
if([[someNotification.userInfo objectForKey:notificationID] isEqualToString:notificationID]){
cancelThisNotification =一些通知;
hasNotification = YES;
break;
}
}
if(hasNotification == YES){
NSLog(@%@,cancelThisNotification);
[[UIApplication sharedApplication] cancelLocalNotification:cancelThisNotification];
}
}
I know there are a few questions here and there regarding how to delete a local notification which might be all or a particular notification.I have also gone through the local notification class reference and found some methods like repeat time interval,fire date,alert body,time zone etc...but I am unable to find out some sort of information regarding how to modify the fire date that is already been set.Say if the user sets a notification with date today and time 4:50 PM,but if the user wishes to modify the set date/time,what's happening is the notification is firing on both occasions.Which is a blunder as far as programming ethics are concerned!
Actually what I want is the previous notification must be cancelled i.e. date must be modified to edited one and notification should be set and fired on the new date.
This is how I set the notification,sample code:
- (void)setNotification
{
//Set notification after confirmation of saved data
Class cls = NSClassFromString(@"UILocalNotification");
reminderNotification = [[cls alloc] init];
if (cls != nil)
{
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *notificationDate = [dateFormat dateFromString:textField2.text];
reminderNotification.fireDate = notificationDate;
reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
NSString *reminderText = [NSString stringWithFormat:@"%@ 's %@ on %@",textField.text,textField1.text,strDate];
reminderNotification.alertBody = reminderText;
reminderNotification.alertAction = @"View";
reminderNotification.soundName = @"lazy_afternoon.mp3";
reminderNotification.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder];
reminderNotification.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification];
[reminderNotification release];
}
}
Can any one please guide me in right path on how to deal with this task.
Thanks all in advance :)
Use this method to schedule Notification , Where the notificationID has to be unique
-(void) scheduleNotificationForDate:(NSDate *)date AlertBody:(NSString *)alertBody ActionButtonTitle:(NSString *)actionButtonTitle NotificationID:(NSString *)notificationID{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.alertBody = alertBody;
localNotification.alertAction = actionButtonTitle;
localNotification.soundName = @"yourSound.wav";
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Use this method to cancel Specific Notification with that Notification Id
- (void)cancelLocalNotification:(NSString*)notificationID {
//loop through all scheduled notifications and cancel the one we're looking for
UILocalNotification *cancelThisNotification = nil;
BOOL hasNotification = NO;
for (UILocalNotification *someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if([[someNotification.userInfo objectForKey:notificationID] isEqualToString:notificationID]) {
cancelThisNotification = someNotification;
hasNotification = YES;
break;
}
}
if (hasNotification == YES) {
NSLog(@"%@ ",cancelThisNotification);
[[UIApplication sharedApplication] cancelLocalNotification:cancelThisNotification];
}
}
Reference : UILocalNotification
这篇关于更新本地通知的日期,并取消以前的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!