Is the user able to disable local notifications? - ios

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];
}

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.

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.

Related

Objective-C: UILocalNotification

ViewController:
- (void)viewDidLoad
{
`[super viewDidLoad];`
dateFormatter.dateFormat = #"dd/MM";
timeFormatter.dateFormat = #"HH:mm";
NSString *dateString = [dateFormatter stringFromDate:dateTime];
NSString *timeString = [timeFormatter stringFromDate:dateTime];
if ([dateString isEqual:#"23/06"]) {
if ([timeString isEqual:#"23:30"]) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
}
AppDelegate:
[[UIApplication sharedApplication] scheduledLocalNotifications];
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
}
The notification isn't receiving when time equals string and date.
Please help!
The core issue with this (aside from using strings to compare dates) is that viewDidLoad gets called only once in this application. Because you are only scheduling the local notification when the current date is 11:30, it will never get called, since you also set the fireDate to the same exact time.
The thing is, local notifications get scheduled prior to the event. In fact, you should set dateTime to the desired schedule time and then set the local notification.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2016];
[dateComponents setMonth:6];
[dateComponents setDay:23];
[dateComponents setHour:23];
[dateComponents setMinute:30];
dateTime = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Based on your uploaded code, it looks like you are going to be grabbing the date from the datePicker, so eventually, you will move the UILocalNotification portion to the button method and use [datePicker date] as the fireDate. Meaning, you can ignore the dateComponents part.

Reset UILocationNotification fire date set for everyday

I've a function in my app which allowed to use for max. 3 times/day to a user – once he/she used it for all 3 times, they can't not use it again until next day.
I'm using UILocationNotification in my app, to show a notification each day. If a user open my app, and he didn't use those functions for that particular day then, I'll set up notification to fire at exactly 4.00pm for that day. But now what I want is, while user is using the app – if he/she used that function for 3 times then the notification should not get fire and it should set for the next day at the same time.
This is how I'm showing up local notification.
- (void) fireNoteForEachDay {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]];
[dateComponents setHour:16];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
[notif setFireDate:[gregCalendar dateFromComponents:dateComponents]];
[notif setAlertBody:#"Come back to the app – to use '<function name>' !"];
[notif setRepeatInterval:NSDayCalendarUnit];
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
If I use all three functions at 3.00pm today – so now it should reset for tomorrow's date at 4.00pm.
How do I do that?
Get tomorrow
NSDateComponents *tomorrowComponents = [NSDateComponents new];
tomorrowComponents.day = 1 ;
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *tomorrow = [gregCalendar dateByAddingComponents:tomorrowComponents toDate:[NSDate date] options:0];
Add time for tomorrow
NSDateComponents *tomorrowAtTimeComponent = [gregCalendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:tomorrow];
tomorrowAtTimeComponent.hour = 16;
tomorrowAtTimeComponent.minute = 0;
NSDate *tomorrowAt4pm = [gregCalendar dateFromComponents:tomorrowAtTimeComponent];
Reset note
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
[notif setFireDate:tomorrowAt4pm];
[notif setAlertBody:#"Come back to the app – to use '<function name>' !"];
[notif setRepeatInterval:NSDayCalendarUnit];
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];

Error in Notification Part

I am working with the notification part of the project, in this I want the sound notification which will notify the user about the activity. The code I am working with is below:
-(void) scheduleNotificationForDate {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateParts = [[NSDateComponents alloc] init];
[dateParts setHour:18];
[dateParts setMinute:20];
[dateParts setSecond:00];
NSDate *sDate = [calendar dateFromComponents:dateParts];
NSLog(#"date : %#", sDate);
localNotif.fireDate = sDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.alertBody = #"Hello Testing";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"notification started");
}
With the help of this code I am getting the written notification on my device but I don't hear any alert sound when notification is alerted.
Please have a look on the code and try to locate my error. Your help will be much appreciable.
Look at your console.
date : 0001-01-01 17:26:32 +0000
You have scheduled the notification ~2000 years in the past, because you didn't specify components for year, month and day. Setting a fireDate that is in the past means the notification is delivered immediately.
If you want to schedule a notification for today at 18:30 you could do something like this:
// get year, month and day components from current time
NSDateComponents *dateParts = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[dateParts setHour:18];
[dateParts setMinute:20];
[dateParts setSecond:00];
May be you have notification sound for this app disabled in Settings/Notification Center?
If your app is in active state it will not give alert sound, put it in background and test it.
NOTE: make sure your system volume is on .

iOS UILocalNotification - Badge Icon and Notification Sound

I have a quick question that should be fairly simple.
First, I am going to start off by posting code:
-(void)notification
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (!localNotification)
return;
// Current date
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day, hour and minutesfor today's date
[components setHour:19];
[components setMinute:30];
[components setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EST"]];
localNotification.fireDate = [calendar dateFromComponents:components];
localNotification.repeatInterval = NSDayCalendarUnit;
[localNotification setAlertBody:#"Your 'Daily Quote' is ready!"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
}
It seems that the localNotification is shown, but the Badge and the sound are never displayed and played. Could it be because this is looping due to "repeatInterval"?
How can I get these displayed? The notification is being displayed at the specified time, but the badge icon is not changed...
Thanks!
-Henry
You should change your code order:
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
Take the code before:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
The same logic is work for me at iOS 5.1 , but the code order.
You reset the badgeNumber and the soundName after the notification apply to the system. NotificationCenter need the info which the local notification object contains,not the object itself,so,in that time,the NotificationCenter can not get the soundName or badgeNumber.Glad to have helped :-)

How to set an Alarm in iOS?

I know this question has asked many times on StackOverflow but i couldn't able to set alarm in my app because i am very new to iOS? I am following this tutorial to set an alarm:
Setting a reminder using UILocalNotification in iOS.
However, it doesn't seems to be working for me.
I am in need to set alarm daily lets say 5.00 PM daily. I can't use date picker for choosing the time.
First on your xib, (or code) set the date picker mode: Time (Default is date & time)
The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:
Local notifications in iOS.
tutorial you posted on how to do this.
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
Call this method with parameters and use this
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hours];
[dateComps setMinute:minutes];
[dateComps setSecond:seconds];
NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;
localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
You may need to change the style of the date picker to allow changing the time in addition to just the date.
You can try this
UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:#"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];

Resources