UILocalNotification launched every minute util user click it - ios

If I set my notification like the code below, it would launch alarm every minute. However, it gets triggered even my alarm is set behind the current time. For example, the current time is 12:00 and the alarm time is 9:00, it still gets triggered because kCFCalendarUnitMinute is a loop.
How can I get the function as my title description?
UILocalNotification *localNotification = [UILocalNotification new];
localNotification.userInfo = #{#"status":#"alarm",#"note":noteString,#"index":#(index)};
localNotification.fireDate = dateToFire;
localNotification.alertTitle = #"Alarm";
localNotification.alertBody = #"Wake Up!";
localNotification.soundName = #"myTone.m4a";
localNotification.alertAction = #"View";
localNotification.repeatInterval = kCFCalendarUnitMinute;
localNotification.regionTriggersOnce = true;
localNotification.applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber + 1;
localNotification.timeZone = [NSTimeZone systemTimeZone];
// To set the local notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Related

Local Notification 15 Minute Interval Issue

I am trying to embed local notification in my code which i want to get repeat after every 15 minutes. It takes NsCalendarUnit and i am unable to figure out as in how to convert 15 mints into NSCalendarUnit
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification.repeatInterval = 900.0;
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
The repeatInterval is not a time, it an value of NSCalendarUnit.
This means you can NOT set the interval to 15 min.
You best option is to use 4 notification with a repeatInterval of NSCalendarUnitHour and set them 15 minutes apart.

Local notification at multiple times like alarm in ios

I am creating an alarm app. I want user set time and Local notification come at that time. For this i am using following code:-
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = _datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"This is the Alert-Body Text";
localNotif.alertAction = #"Button-Text";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
But the problem is notification comes only once and how i set multiple times for local notification so that it comes for every alarm.

Difference between push notification and nsnotifcation centre in iOS sdk

I have question about push notification and the Notification Center. I have built an app and now I want to support notification the the time hits 0 I'm doing like countdown app shows the release date for games and title.
Should I use nsnotifcation center or push notification
You dont need Push Notifications you can acheive that with UILocalNotification. For Push Notifications you need coordination with server but for UILocalNotification you can do it inside your application so better try to use UILocalNotification
For simple UILocalNotification
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
For UILocalNotification with timers
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
you can study in detail in here http://www.appcoda.com/ios-programming-local-notification-tutorial/

Create a daily local notification that will start tomorrow

I have an app that reminds user about a todo item every day but I do not not need to remind them if they already did it.
My approach to solve the problem is by creating a local notification that is fired up in a daily basis at a random time. When a todo item is already done for the day, I re-scheduled the local notification day to tomorrow by cancelling it and creating a new one dated for tomorrow. But this solution doesn't work. It seems like when you specify that a local notification is fired daily (repearInterval=NSDayCalendarUnit), it doesn't honour the fireDate day property.
Here is my code:
/*** notification is created like this ***/
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [self randomHourMinFromDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"A todo item is due!";
localNotif.alertAction = #"View Item";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSDayCalendarUnit;
/*** some other part of the code ***/
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notification = [scheduledNotifications firstObject];
// adds 1 day to notification fireDate
NSDate *newFireDate = [notification.fireData dateByAddingTimeInterval:60*60*24*1];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
notification.fireDate = newFireDate;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Can you please tell me what I'm doing wrong? Or perhaps recommend a better solution? TIA!
I have a idea. When a todo item is already done for the day, cancel the notification and create a new one. Set firedate to the current date with same time, and set dateByAddingTimeInterval: 60*60*24.
localNotification.fireDate = [CurrentDate dateByAddingTimeInterval:60*60*24];
May this could work.

Adding seconds to a self.timepicker date (for localnotification purposes)

I want to know if it is possible to add seconds to a local notification? I am trying to create a loop to schedule local notifications 30 seconds after each others. So, in the loop below, can I keep on "delaying" the firedate 30 seconds after each other. I don't know if that question makes sense, but that's the best I can describe my problem as. Think of it as a 30 second interval, but manually scheduling each notification.
for (notifs = 1, notifs <= 64, notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [self.timePicker date];
//can i write it like [self.timePicker date] + 30000?
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
}
Thanks.
You could use NSDate's dateByAddingTimeInterval: and just pass the current iteration number multiplied by 30.
for (notifs = 1; notifs <= 64; notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[self.timePicker date] dateByAddingTimeInterval:notifs * 30.0];
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Resources