我通过在按钮操作设置警报上调用以下方法来根据 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 通知。如果有人知道,请帮助我。提前致谢。