Reputation: 97
I have created an app that will give the user a daily notification at 9am, but want the user to be able to turn it off if they wish.
i have set up a settings bundle for the app, and set up a slider for enableNotifications. When i build and test the app, the settings are there and I can turn them on or off...but it doesnt seem to be registering it in the app. How do I program the app to check whether or not the notification setting is on or off?
I saw this answer: Determine on iPhone if user has enabled push notifications but I dont know where to put it in and program it properly (if/else statements, etc)
Here is the code where the notification is created:
-(id) getCommandInstance:(NSString*)className
{
/** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
* own app specific protocol to it. -jm
**/
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
fromDate:currentDate];
NSDateComponents *temp = [[NSDateComponents alloc]init];
[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour: 22];
[temp setMinute:00];
NSDate *fireTime = [calender dateFromComponents:temp];
[temp release];
// set up the notifier
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = fireTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"Don't Forget Your 365 Day Photo Challenge!";
localNotification.alertAction = @"LAUNCH";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[localNotification release];
return [super getCommandInstance:className];
}
Upvotes: 3
Views: 2459
Reputation: 212
Based on your description, I think you are asking how to retrieve the value of the notification setting from your app's preference, i.e. whether notification should be enabled or disabled.
You should use [NSUserDefaults standardUserDefaults] to access preference values. Please refer to this Accessing Preference Values document.
Upvotes: 0
Reputation: 18517
If you have just one localNotification and want to cancel it you can just call:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
This will not throw an error if there aren't any localNotifications. You can call this through a button press IBAction. If you want to set the state of a switch based on whether there are any localNotifications then you can you can check to see it there are any:
NSArray* localNotifications = [[UIApplication sharedApplication]
scheduledLocalNotifications];
If localNotifications.count > 0 then you have some notifications scheduled.
Upvotes: 1