繁体   English   中英

iPhone中的本地通知无法取消

[英]Local notification not cancelling in iphone

我正在创建一个提醒应用程序。在特定时间触发本地通知。我还实现了一个用于打开/关闭通知的功能。通知功能正常,但取消功能不起作用。下面的代码。此代码有什么问题。请帮助我。谢谢。

-(void)机智{

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");
}

}

如果由于您的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;

当您检测到flag==1 ,为其分配另一个值,因此下次您的通知将被取消。 同样不要忘记在else部分中将flag放回1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM