【问题标题】:How do I cancel a notification only?如何仅取消通知?
【发布时间】:2013-04-20 17:59:04
【问题描述】:

我想做点什么。我的通知设置为 11 和 12 小时。我想取消订阅这些设置的通知仅在 11 小时。我该怎么做?

我的代码;

NSCalendar *greg = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents * component = [greg components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
        [component setYear:2013];
        [component setMonth:m];
        [component setDay:d];
        [component setHour:x];
        [component setMinute:y];

        UIDatePicker *dd = [[UIDatePicker alloc]init];
        [dd setDate:[greg dateFromComponents:component]];

        UILocalNotification * bildirim = [[UILocalNotification alloc]init];
        [bildirim setAlertBody:newtask.name];
        [bildirim setFireDate:dd.date];
        bildirim.soundName = UILocalNotificationDefaultSoundName;
        bildirim.alertAction = NSLocalizedString(@"Test", nil);
        //[bildirim setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];
        [bildirim setTimeZone:[NSTimeZone defaultTimeZone]];

        [[UIApplication sharedApplication] scheduleLocalNotification:bildirim];

我找到了一个代码。不幸的是,此代码正在删除所有通知。

[[UIApplication sharedApplication] cancelAllLocalNotifications];

对不起,我的英语不好。

【问题讨论】:

    标签: iphone ios xcode localnotification


    【解决方案1】:

    在创建 local notification 时有两件事,请使用一些唯一标识创建它,以便您可以像下面这样进行标识

    UILocalNotification * notification = [[UILocalNotification alloc]init];
    notification.userInfo=[NSDictionary dictionaryWithObject:@"UNIQUE_ID" forKey:@"notifcation_id"];
    

    然后要取消具体的需要以下步骤

    for(UILocalNotification *notificaiton in [[UIApplication sharedApplication] scheduledLocalNotifications]){
    
        if([[notificaiton.userInfo objectForKey:@"notifcation_id"] isEqualToString:UNIQUE_ID]){
            [[UIApplication sharedApplication] cancelLocalNotification:notificaiton];
            break;
        }
    }
    

    一切顺利..

    【讨论】:

    • 尝试:pastie.org/7675392 错误:使用未声明的标识符“eventID”。什么是事件ID? :S
    • 你不能让这段代码运行,你需要定义你自己的UniqueID/EventID..并且使用这个ID你可以追踪你想要取消的通知..
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多