更新本地通知的日期,并取消以前的通知 [英] Update fire date for local notification and cancel previous notification

查看:87
本文介绍了更新本地通知的日期,并取消以前的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个问题此处有关如何删除可能是全部或特定通知的本地通知。还经历了本地通知类参考,并发现一些方法,如重复时间间隔,火灾日期,警报体,时区等...但是我无法找到有关如何修改已经设置的火灾日期的某些信息。如果用户今天和时间4:50 PM设置一个通知,但是如果用户希望修改设定的日期/时间,那么通知正在两次触发,这是一个错误的编程伦理学重新关注!



其实我想要的是先前的通知必须被取消,即日期必须修改为编辑的,通知应该在新的日期设置和触发。 / 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];
}
}

参考:
UILocalNotification


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆