Reputation: 1427
I have set UILocalNotification according UISWitch position by calling following method on button action set alarm.
- (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");
}
}
I write following code in AppDelegate. Here also i check switch position.
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");
}
Now my problem is when I set Notification according to time. It is getting alert on time. But in between when i make Switch OFF and again ON it is not getting alert. There is any way to make On and Off notification according to switch position. Please help me if any one knows. Thanks in advance.
Upvotes: 0
Views: 1037
Reputation: 3024
to turn off you notification you can use [[UIApplication sharedApplication] cancelAllLocalNotifications];
or for specific [[UIApplication sharedApplication] cancelLocalNotification:<#(UILocalNotification *)#>];
and then you will have to reschedule your notification like this
[[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];
In my case i am rescheduling for one day later
Upvotes: 4
Reputation: 5314
If there can only be one UILocalNotification
at a time, I would declare it as a variable so you can access it at will.
If you store your UILocalNotification
you can use a combination of -cancelLocalNotification:
to turn it OFF and -scheduleLocalNotification:
to turn it back ON, while keeping the properties of it intact.
Upvotes: 1