取消UILOcalNotifications

时间:2014-12-08 07:35:28

标签: ios objective-c uilocalnotification

我在我的应用程序中使用UILocalNotifications。现在我的要求是本地通知应该在安排后取消。

这是我的代码...

-(void)LocalNotificationMethod{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate =  self.selectedDate;
    NSLog(@" self.selectedDate %@", self.selectedDate);
    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];

    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    // Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    NSLog(@"itemDate %@",itemDate);
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
      NSLog(@"itemDate %@", localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    // Notification details
    localNotif.alertBody = [_titleTextFieldObj text];
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber =[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    NSLog(@" localNotif.applicationIconBadgeNumber ++ %ld", (long)localNotif.applicationIconBadgeNumber );



    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[_titleTextFieldObj text] forKey:@"someKey"];

    localNotif.userInfo = infoDict;
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
    NSLog(@"notif %@",notificationArray);


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


   }

**In Appdelegate.m i wrote the code like…**

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
        application.applicationIconBadgeNumber=1;
           if (notification) {
            NSLog(@"notify %@",notification);
            NSString *custom=[notification.userInfo objectForKey:@"someKey"];
             NSLog(@"custom %@",custom);
             NSString *newString = [custom stringByReplacingOccurrencesOfString:@" " withString:@""];

             NSLog(@"newString %@",newString);

            NSLog(@"custmky%@",notification.description);
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:newString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            alert.delegate=self;
            [alert show];


        }
        //UIApplication *application = [UIApplication sharedApplication];
        NSString *notificationId = @"id_to_cancel";
        //UILocalNotification *notification = nil;
        for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
        {
            if([[notify.userInfo objectForKey:@"someKey"] isEqualToString:notificationId ])
            {
                notification = notify;
                break;
            }
        }
        [[UIApplication sharedApplication] cancelLocalNotification:notification];


    }

但它没有进入循环......

我是这个概念的新手。任何人都可以帮我解决这个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您希望根据接收到另一个通知来取消预定通知,如果您需要在显示警报之前放置循环,否则您的循环将永远不会被执行。或者如果您需要取消/不通知将在警报视图中录制的按钮,您可以将该代码放入alertView委托

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex==0) {

        for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications]){

            if([[notify.userInfo objectForKey:@"someKey"] isEqualToString:@"someValue"]) {
                [[UIApplication sharedApplication] cancelLocalNotification:notify];
                break;
            }
        }

}

答案 1 :(得分:0)

默认情况下,所有UILocalNotification都是OneTime,因此会在fireDate

之后自动取消

看起来你正试图获得同样的UILocalNotification

请尝试在不同的地方放置相同的代码,以获得所有Scheduled Notifications或尝试在Scheduled Notifications之前获取所有fireDate