send local push - From Date to To Date with interval of hours - ios

Below is the code I am using to send local push.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
NSString *alertText = [NSString stringWithFormat:#"Test Push Title"];
localNotification.alertBody = alertText;
localNotification.soundName = #"Glass.caf";
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];
But now I want to send push from to date with interval of hours.
E.g. I want to send push from 1-Jan-2014 to 2-Feb-2014.
Now daily I will have 3 push. Each push will start at 8 pm and in a day after 6 after I will get rest push. Means first push at 8am, second at 2pm and 3rd push at 8 pm.
Any idea how to do this?
Any guidance would be greatly appreciated.
Actually I want to do the reminder where I will have below inputs.
Daily how many times you will have medicine
From what time you will have medicine
After how many interval you will have another medicine
For how long days you will have medicine
Edit 1
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
NSTimeInterval interval = 1*60;
for (int i =0;i<=2;i++ ) {
// setup the notification using the fire date
// pickerDate = [pickerDate dateByAddingTimeInterval:interval];
pickerDate = [pickerDate dateByAddingTimeInterval:interval];
}
I tried with this code, but still I am getting one push. I was expecting 4 pushes...

You can have up to 64 scheduled notification so be aware that there is a limit.
You can put your above code into a loop and add the interval date components (using a calendar) or the interval seconds (dateByAddingTimeInterval) onto the fire date of the previous notification.
Aside, this is pointless:
[NSString stringWithFormat:#"Test Push Title"]
And wasteful. There is no format, it's just a string so just directly use #"Test Push Title".
NSDate *fireDate = pickerDate;
NSTimeInterval interval = ...;
for ( however you decide how many notifications there are ) {
// setup the notification using the fire date
fireDate = [fireDate dateByAddingTimeInterval:interval];
}

Related

UILocalNotification with custom repeat Intervals other than NSCalendarUnits [duplicate]

I am trying to setup a UILocalNotification to run every 30 seconds using the following logic, however it seems to be misbehaving. There are 2 issues:
When the notifications get fired there seems to be a lot of them all at once, rather than 1 every 30 seconds.
The application icon badge number doesn't seem to increase. It just stays at 1.
Please can someone help me work out what I've done wrong?
// Create 'base' notification we can use
UILocalNotification *baseNotification = [[UILocalNotification alloc] init];
baseNotification.timeZone = [NSTimeZone defaultTimeZone];
baseNotification.repeatInterval = NSMinuteCalendarUnit;
baseNotification.alertBody = #"My Message.";
baseNotification.alertAction = #"My Alert Action";
baseNotification.soundName = UILocalNotificationDefaultSoundName;
UILocalNotification *alertOne = [baseNotification copy];
alertOne.applicationIconBadgeNumber++;
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:alertOne];
UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.applicationIconBadgeNumber++;
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
[[UIApplication sharedApplication] scheduleLocalNotification:alertTwo];
Try this one.
UILocalNotification *baseNotification = [[UILocalNotification alloc] init];
baseNotification.timeZone = [NSTimeZone defaultTimeZone];
baseNotification.repeatInterval = NSMinuteCalendarUnit;
baseNotification.alertBody = #"My Message.";
baseNotification.alertAction = #"My Alert Action";
baseNotification.soundName = UILocalNotificationDefaultSoundName;
UILocalNotification *alertOne = [baseNotification copy];
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
alertOne.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
alertTwo.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
There is currently no way possible to achieve custom repeats with intervals.
However, the notification system can queue up to 64 notifications so the closest thing you could do is to manually set as many notifications as you need (with each one having a different number for the badge and a different fireDate) and then have your notifications list updated by setting new ones when you're running low on them.
This will return how many notifications you've in queue:
[[[UIApplication sharedApplication] scheduledLocalNotifications] count]
There's also this post that I would recommend you reading for further help:
iOS badge number live update
Good luck!
Regarding second point, you're increasing the badge number of the copy not the original notification. And since the original has a zero badge number you'll always get a copy with zero badge number too and increasing it will make it always 1.
The solution is to increase the badge of the original notification right before making the copy:
...
baseNotification.applicationIconBadgeNumber++;
UILocalNotification *alertOne = [baseNotification copy];
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:alertOne];
baseNotification.applicationIconBadgeNumber++;
UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
[[UIApplication sharedApplication] scheduleLocalNotification:alertTwo];
According NSObject class reference :
copy - Returns the object returned by copyWithZone:
And copyWithZone returns a shallow copy.
So its like all notification have same properties .
Hence , badge number is always "1" and fireDate is same for all notifications. i.e. last one that you apply .
Hence , notifications get fired at same time.
Hope , it helps.
I think you get a lot of notifications every 30 seconds is because you did not cancel previous notifications. Add this line at the top of your code.
[[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification with repeat-once behaviour like in Messages App

I have simple UILocalNotification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Message";
notification.alertAction = #"Action";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.category = kCategoryIdentifier;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Is it possible, to repeat notification once, for example after two minutes? I want behaviour exacly, like in Messages app.
I have tried to set repeatInterval property of notification object, but:
Notification will be presented to user every two minutes, not repeated only once
System shows to user new notification, not repeat the old one. User see two notifications, one with timestamp 2 minutes after another.
Which is not what I've expected.
Also, because of second reason, I don't want to schedule two separate notifications.
Edit: In my app time when something happend is very important. Because of that, in lock screen, when notification is repeated, I want user to know that is something that happend earlier, not in time when notification arrives. So repeated notification should have timestamp of first notification.
Yes, you can set repeatInterval.
See documentation here
The calendar interval at which to reschedule the notification.
Declaration SWIFT var repeatInterval: NSCalendarUnit OBJECTIVE-C
#property(nonatomic) NSCalendarUnit repeatInterval
try this code
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"Message";
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber=1;
NSLog(#"LocalNotif.soundName %#",localNotif.soundName);
for (int i=0; i<20; i++)
{
localNotif.fireDate = [repeatAlarm dateByAddingTimeInterval:120*i];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Issue with scheduling 4 subsequent local notification in ios

I'm trying to create 4 different local notifications that are separated by a static time difference. Means, the first notification will fire at the start time and then each X seconds another local notification will fire ( 4 total).
The problem that only 2 local notifcaiotns are creates and not 4 . I have no idea why.
double timeToAdd=0;
double timeDifference=[album.end_time timeIntervalSinceDate:album.start_time]/4;
for (int i = 1; i <= 4; i++)
{
NSDate *dateToNotify = [NSDate dateWithTimeInterval:timeToAdd sinceDate:album.start_time];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateToNotify;
localNotification.alertBody = #"Click to sync";
localNotification.alertAction=#"Click to sync";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo =#{#"id":[ album.id stringByAppendingString:album.code]};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
timeToAdd+=timeDifference;
}

How to post a local notification prior to 5 minutes before the actual time of the remainder in iOS program?

I've created a reminder in my app, I want the remainder to be notified before 5 minutes of its actual time. If yes, is there any default available for doing it?
Thanks!
For this you can add two reminders, one for actual time and other 5 minutes before the actual time. Now you can do different things on receiving both reminders..
you can remove 5 min by creating new date from your date by
NSDate *fiveMinutesBeforeDate = [NSDate dateWithTimeInterval:-60*5 sinceDate:dateFromFirstString];
and create local notification by following cate and set this date to firedate of local notification
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fiveMinutesBeforeDate;
localNotification.alertBody = [NSString stringWithFormat:#"Alert Fired at %#", fiveMinutesBeforeDate];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

custom ui local notification by user setting

I know how to make a local notification in the applicationDidEnterBackground function in AppDelegate.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification * uln = [[UILocalNotification alloc] init];
uln.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
uln.timeZone = [NSTimeZone defaultTimeZone];
uln.alertBody = #"Did you forget something?";
uln.alertAction = #"Show me";
uln.soundName = UILocalNotificationDefaultSoundName;
//uln.applicationIconBadgeNumber = 0;
uln.repeatInterval = NSMinuteCalendarUnit;
application.scheduledLocalNotifications = [NSArray arrayWithObject:uln];
}
But is that possible a user can set a time for the fireDate and a do not disturb time like late in midnight?
Yes you can set the firedate property to any date in the future. Do not disturb is possible for the user to trigger himself in Settings in iOS6.

Resources