【问题标题】:Update fire date for local notification and cancel previous notification更新本地通知的触发日期并取消之前的通知
【发布时间】:2012-06-23 14:57:24
【问题描述】:

我知道herethere 有几个关于如何删除可能是全部或特定通知的本地通知的问题。我也浏览了local notification class reference 并找到了一些方法,如重复时间间隔、火灾日期、警报正文、时区等......但我无法找到有关如何修改火灾日期的信息已经设置好了。假设用户使用今天的日期和下午 4:50 的时间设置通知,但如果用户希望修改设置的日期/时间,则发生的情况是通知在两种情况下都被触发。就编程道德而言,这是一个错误!

实际上我想要的是必须取消以前的通知,即必须将日期修改为已编辑的通知,并且应在新日期设置和触发通知。

这是我设置通知的方式,示例代码:

- (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];
    }
}

如何处理这个任务?

【问题讨论】:

    标签: iphone date uilocalnotification


    【解决方案1】:

    使用此方法安排 Notification ,其中 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];
    }
    

    使用此方法取消具有该通知 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];        
        }
    }
    

    参考: UILocalNotification

    【讨论】:

    • 一个小疑问,我可以在计划通知中声明通知 ID,而不是将其放在方法本身中:)
    • 因为我需要在保存按钮操作中调用 [self scheduleNotification],如果我这样写,那么如何访问所有这些变量并将其放入方法本身:(
    • 让我考虑你在 [self scheduleNotification] 方法中有 fireDate 和 alertBody 内容,所以你可以将上面的方法重写为 -(void) scheduleNotificationForNotificationID:(NSString *)notificationID;忽略您已经拥有的那些参数,您可以在保存按钮中将该方法保存为 [self scheduleNotificationForNotificationID:@"yourUniqueId"];
    • 哦,你的意思是说即使我只是调用 [self setNotification] 而没有调用这个方法,我的意思是 void scheduleNotification 方法,通知被设置
    • 是的,您最好更改此代码 NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder];提醒通知.userInfo = userDict;在你的 setNotification 方法中,就像这样 NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:self.textField1.text]; localNotification.userInfo = infoDict;
    【解决方案2】:

    一旦你设置了通知,编辑它的唯一方法就是取消旧的并重新创建另一个,所以你可以这样做,搜索你现有的并取消它。

    for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
            if([[aNotif.userInfo objectForKey:@"id"] isEqualToString:nId]) {
                [[UIApplication sharedApplication]cancelLocalNotification:aNotif];
            }
        }
    

    然后创建新的通知。

    【讨论】:

    • 对不起,我无法理解这里的关键是什么,我应该为每个通知创建一个 id,如果是如何设置,请详细说明,我是本地通知的新手,请参阅我的关于我如何设置通知的代码,谢谢:)
    • 很简单,你只需要一个唯一的密钥,你生成并存储它,你可以使用它,通知和使用相同的密钥取消它。如果你有一个通知,你可以存储它在 NSUserDefaults 中,否则您需要多个条目的数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多