如何开启/关闭iPhone中的UILocalNotification?

时间:2012-06-09 07:28:51

标签: iphone uilocalnotification

我通过在按钮动作设置警报上调用以下方法,根据UISWitch位置设置了UILocalNotification。

- (void)scheduleNotificationWithInterval:(int)minutesBefore {

    NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
    [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [theDateFormatter setDateFormat:@"EEEE"];
    NSString *currentDay=  [theDateFormatter stringFromDate:combo2.datePick.date];

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = [combo2.datePick.date dateByAddingTimeInterval:-30];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.repeatInterval=NSCalendarCalendarUnit;
    localNotif.repeatInterval = NSDayCalendarUnit;

    if (iBOfSwitchAlarm.on) {

        if([combo1.selectedText isEqualToString:currentDay])
        {

            NSLog(@"In DayCheck");
            localNotif.alertBody = @"Alarm Test";
            localNotif.alertAction = @"Ok";
            localNotif.soundName = UILocalNotificationDefaultSoundName;

            NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"THere Message for Alarm" forKey:@"Message"];
            localNotif.userInfo = infoDict;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        }
    }else{
        NSLog(@"SwOff");
    }
}

我在AppDelegate中编写以下代码。在这里我也检查开关位置。

ViewController *obj =[[ViewController alloc]init];

    NSString *itemName = [notif.userInfo objectForKey:@"Message"];

    if (obj.iBOfSwitchAlarm.on) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm Title" message:itemName delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Continue", nil];
        alertView.tag = 1111;
        [alertView show];

        app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1;
    }else{
       NSLog(@"SwitchOff");
    }

现在我的问题是我根据时间设置通知。它准时得到警觉。但是在我关闭并再次打开之间它没有得到警报。有任何方法可以根据开关位置进行On和Off通知。如果有人知道,请帮助我。提前致谢。

2 个答案:

答案 0 :(得分:4)

要关闭通知,您可以使用[[UIApplication sharedApplication] cancelAllLocalNotifications];或特定[[UIApplication sharedApplication] cancelLocalNotification:<#(UILocalNotification *)#>]; 然后你将不得不重新安排你的通知

[[MKLocalNotificationsScheduler sharedInstance] scheduleNotificationOn:[[NSDate Date] addTimeInterval:60*60*24*1]
                                                                  text:@"Hi this is notification"
                                                                action:@"View"
                                                                 sound:@"Raising_Sound.mp3"
                                                           launchImage:nil
                                                               andInfo:nil];

就我而言,我将在一天后重新安排

答案 1 :(得分:1)

如果一次只能有一个UILocalNotification,我会将其声明为变量,以便您可以随意访问它。

如果您存储了UILocalNotification,则可以使用-cancelLocalNotification:组合将关闭-scheduleLocalNotification:将其关闭开启,同时保持其完整性。