3

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];
}
Community
  • 1
  • 1
Noel Chenier
  • 97
  • 1
  • 1
  • 13
  • upon further research I came across this, which I think is what I need to do: http://useyourloaf.com/blog/2010/05/18/adding-a-settings-bundle-to-an-iphone-app.html However, I'm not sure what to change in this code: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Get user preference NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; BOOL enabled = [defaults boolForKey:@"enableRotation"]; if (enabled) { return YES; } else { return (interfaceOrientation == UIInterfaceOrientationPortrait); } } – Noel Chenier Aug 24 '12 at 11:41

2 Answers2

1

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.

spring
  • 18,009
  • 15
  • 80
  • 160
  • Hi SkinnyTOD How would I implement that as an IF/THEN statement? – Noel Chenier Aug 26 '12 at 18:43
  • @user1114118: I don't understand your question. – spring Aug 26 '12 at 18:55
  • sorry. my coding knowledge is pretty limited. My understanding is that I need to have it: 1) Check whether or not the user has turned local notifications on or off in the settings bundle 2) if they have them on, set up the notification. 3) If they don't, do nothing. I guess i don't know what I need to do to implement that in the code. – Noel Chenier Aug 26 '12 at 19:53
  • The first place to start is with the docs: http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194 - the programming itself isn't that hard and there are many tutorials. The challenge is in the details: the various states your app can be in (active, not running, notifications off,etc.) and how you handle the notification for each state. I suggest making a test project to focus on this. Trying to rush through it, copying code you don't understand is going to lead to wasted effort. – spring Aug 26 '12 at 20:09
0

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.

K S
  • 212
  • 1
  • 1
  • Thank you both! I was able to set it up this way: if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableNotifications"]) { CODE TO ENABLE NOTIFICATIONS } else {[[UIApplication sharedApplication] cancelAllLocalNotifications];} return [super getCommandInstance:className]; } – Noel Chenier Aug 26 '12 at 23:42