开发者

Local notification not cancelling in iphone

开发者 https://www.devze.com 2023-03-24 01:13 出处:网络
I\'m creating a reminder app.In that a local no开发者_开发问答tification is firing at particular time.I\'m also implementineg a function for switching ON/OFF the notification.Notification is working f

I'm creating a reminder app.In that a local no开发者_开发问答tification is firing at particular time.I'm also implementineg a function for switching ON/OFF the notification.Notification is working fine but cancel function is not working.I'm using the below code.Is there any thing wrong in this code.Please help me.Thanks in advance.

-(void)swit {

if (flag==1) {

//if (toggleSwitch.on) {
    toggleSwitch.on=YES;
    flag=1;




    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate = [self.datePicker date];
    // 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];
    [dateComps release];


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

    // Notification details
    localNotif.alertBody = @"Tip of the day";
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];



    NSLog(@"it is working");
}

else {

    toggleSwitch.on=NO;


    [[UIApplication sharedApplication] cancelLocalNotification:localNotif];





   NSLog(@"it is not working");
}

}


The reason why the notification gets never cancelled if due to your flag var :

// so if flag == 1
if (flag==1) {
    toggleSwitch.on=YES;
    // the value doesn't change
    // you will never reach the else part
    flag=1;

When you detect flag==1, assign it another value, so the next time your notification will be cancelled. Also don't forget in the else part, to put flag back to 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消