Local notifications are repeat on every day and every month - ios

I have a Local Notifications. Like this ;
NSNumberFormatter *faturaSonOdemeGunuFormatter = [[NSNumberFormatter alloc] init];
[faturaSonOdemeGunuFormatter setNumberStyle:NSNumberFormatterNoStyle];
NSNumber *faturaSonOdemeGunuNumber = [faturaSonOdemeGunuFormatter numberFromString:_txtFaturaGunu.text];
NSDate *currentDate = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:currentDate];
[components setTimeZone:[NSTimeZone systemTimeZone]];
[components setHour:12];
[components setMinute:40];
[components setDay: faturaSonOdemeGunuNumber.integerValue -3];
NSDate *test = [calendar dateFromComponents:components];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = test;
localNotification.alertBody = [NSString stringWithFormat:#"%# Faturanıza 3 gün kaldı.",_txtFaturaAdi.text];
localNotification.alertAction = #"Faturayı göster";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
This code does not repeat itself.I want to repeat Local Notification every Month in faturaSonOdemeGunuNumber integerValue day.It should be start sonOdemeGunuNumber -3 (for example user writes 17. it should start 14) and it should give a notification on every day until faturaSonOdemeGunuNumber. I mean it should continue notify the users every day until selected day.
How can i do that?
Thank you!

Add this:
localNotification.repeatInterval = kCFCalendarUnitMonth; //For monthly repeats
localNotification.repeatInterval = kCFCalendarUnitDay; //For daily repeats

Try this,
notification.repeatInterval= NSDayCalendarUnit; //For day
notification.repeatInterval= NSWeekCalendarUnit; //For Week
notification.repeatInterval= NSMonthCalendarUnit; //For Month
Ref Link: http://useyourloaf.com/blog/2010/09/13/repeating-an-ios-local-notification.html

Related

How to notify a notification message at every month end day

How to notify every month notification.
-(void)applicationDidEnterBackground:(UIApplication *)application {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"dd-MM-yyyy";
NSComparisonResult result = [[format stringFromDate:[NSDate new]] compare:[format stringFromDate:[self lastDayOfMonthOfDate:[NSDate date]]]];
if (result == NSOrderedSame){
if (![USERDEFAULTS boolForKey:#"IS_MONTH"]) {
[self getNotifiedForEveryMonth:nil];
}}
else{
[USERDEFAULTS setBool:NO forKey:#"IS_MONTH"];
}
}
// getNotifiedForEveryMonth
-(void)getNotifiedForEveryMonth:(id)userinfo{
// Schedule the notification
[USERDEFAULTS setBool:YES forKey:#"IS_MONTH"];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = #"Every Month Notificiation";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
}
-(NSDate*)lastDayOfMonthOfDate:(NSDate *)date
{
// NSGregorianCalendar
NSInteger dayCount = [self numberOfDaysInMonthCountForDate:date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDateComponents *comp = [calendar components:
NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay fromDate:date];
[comp setDay:dayCount];
return [calendar dateFromComponents:comp];
}
-(NSInteger)numberOfDaysInMonthCountForDate:(NSDate *)date
{
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:TIME_ABSOLUTE]];
NSRange dayRange = [calendar rangeOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitMonth
forDate:date];
return dayRange.length;
}
The above code with i try to notify the notification.
Its getting notification every month end day,
If user close the notification and then next month comes the notifcation is not notifying.
After installation the application, how to notify the user very month end day a notification message.
Your inputs are appreciated.
1) If you want fire notifications every month, even if user didn't open an app more then month, you should set several notifications (up to 60). For example, set notifications for next 12 month.
2) You should set notifications by this code (on loop, 12 times):
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
3) Before you set new notifications, remove old with this code:
[[UIApplication sharedApplication] cancelAllLocalNotifications];

Show local notification on specific day and specific time in iOS

i want to show local notification on every saturday like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=#"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
NSLog(#"day: %#", dayString);
if([dayString isEqualToString:#"Saturday"])
{
NSLog(#"Success");
[self PushNotification];
}
-(void)PushNotification
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = #"Test";
localNotification.alertBody =#"test of notification";
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
[componentsForFireDate setWeekday: 7]; //for fixing Saturday
[componentsForFireDate setHour: 17]; //for fixing 5PM hour
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;
}
but my local notification is display in every minit then how can i display notification on every Saturday of the week.
thanks.
Try to declare fireData property in localNotification and schedule your notification
localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Hope this help

UILocalNotification with repeatInterval set NSWeekdayCalendarUnit doesn't trigger weekly

I'm trying to setup local notification to repeat weekly.
Here's my setup:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
...
localNotification.repeatInterval = NSWeekdayCalendarUnit;
In console log:
localNotif: {fire date = Friday, April 24, 2015 at 12:27:33 PM Singapore Standard Time, time zone = Asia/Singapore (GMT+8) offset 28800, repeat interval = NSWeekdayCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, April 25, 2015 at 12:27:33 PM Singapore Standard Time, user info = {
KUserLocalNotficationKey = "2015-04-24 04:27:33 +0000";
}}
As you can see, the next fire date is trigger on the next day. Is this a bug?
I've tested it in iOS 7.1, 8.1, 8.3.
you have to code like this
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
...
localNotification.repeatInterval = NSCalendarUnitWeekOfYear;
It trigger notification on weekly bases.
PLz Try this
UILocalNotification *localNotif=[[UILocalNotification alloc]init];
localNotif.fireDate =currentDate;
localNotif.timeZone=[NSTimeZone defaultTimeZone];
localNotif.alertBody = #"MazeRoll";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval=NSWeekCalendarUnit;
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotif];
NSLog(#"sdfsdfsdf%#",[[UIApplication sharedApplication] scheduledLocalNotifications]);
You can set local notification with NSCalendarUnitWeekOfYear and setting up proper fire date.
UILocalNotification *localNotification=[[UILocalNotification alloc]init];
localNotification.fireDate =[NSDate date]; // set your week day on which repeatedly you want local notfication. for example Monday, then set Fire date of next monday.
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.repeatInterval=NSCalendarUnitWeekOfYear;
NSLog(#"%#",localNotification);
Hope this helps. Enjoy Coding !!
NSDate *currentDate = [NSDate date];
NSDate *futureTime = [currentDate dateByAddingTimeInterval:60*60*168];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:futureTime];
if ([components hour] >= 19) { // make it the next day
[components setDay:[components day] + 1 ];
}
[components setHour:8];
[components setMinute:00];
NSDate *alertTime = [calendar dateFromComponents:components];
NSLog(#"alertTime %#",alertTime.description);
UILocalNotification *localNotif=[[UILocalNotification alloc]init];
localNotif.fireDate =alertTime;
localNotif.timeZone=[NSTimeZone defaultTimeZone];
localNotif.alertBody = #"MazeRoll";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval=NSDayCalendarUnit;
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotif];
NSLog(#"sdfsdfsdf%#",[[UIApplication sharedApplication] scheduledLocalNotifications]);

How to allocate custom time in UILocalnotification?

- (void)scheduleNotification :(int) rowNo
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.timeZone = [NSTimeZone defaultTimeZone];
NSString *descriptionBody=[[remedyArray objectAtIndex:rowNo]objectForKey:#"RemedyTxtDic"];
NSLog(#"%#",descriptionBody);
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
forKey:#"kRemindMeNotificationDataKey"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
I have a column name frequency which is fetched from Sqldb where it contains the number of times notification should appear for a particular cell.
if frequency = 3 ..the notification should fire say 8 AM , 2PM then 8PM
if frequency = 4 ..the notification should fire say 8 AM , 12PM , 4PM then 8PM.
Is there a way to do it? If anyone can help me that would be great
Unfortunately, You can specify value for repeatInterval only of type NSCalendarUnit (day, week, month). So, I think, You need to create several Notifications with different fireDate, and specify for them repeatInterval = NSDayCalendarUnit
For example,
NSDate *currentTime = [NSDate date];
notification1.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL sinceDate:currentTime];
notification1.repeatInterval = NSDayCalendarUnit;
notification2.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL * 2 sinceDate:currentTime];
notification2.repeatInterval = NSDayCalendarUnit;
After user viewed some of notifications - You can cancel them.
Update.
You can also create NSDate from different components like this:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first Monday in the month
[components setMonth:5]; // May
[components setYear:2013];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
Also You can set hour, minutes, seconds, timezone and other parameters.

iPhone : Daily local notifications

I am trying to implement local notification
This is what I have set
// Current date
NSDate *date = [NSDate date];
// Add one minute to the current time
NSDate *dateToFire = [date dateByAddingTimeInterval:20];
// Set the fire date/time
[localNotification setFireDate:dateToFire];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
Instead of 20, I want to put a fixed time(daily)to start push.
For ex:I want to push notification pop up at every 6:00AM.
How can do that ?
Thanks
You just need to properly create a NSDate object to be your fire date (time). Instead of using [NSDate dateByAddingTimeInterval: 20], use something like this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: 6];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
Here are the Apple NSDateComponents API docs
And then when you add the date to the notification, set the repeat interval to one day:
[localNotification setFireDate: dateToFire];
[localNotification setTimeZone: [NSTimeZone defaultTimeZone]];
[localNotification setRepeatInterval: kCFCalendarUnitDay];
As with all date related code, make sure to test how this works during the switch to daylight savings time, if your time zone uses daylight savings time.
I guess what you need is NSDayCalendarUnit.
You can check this answer. And here is another tutorial worth reading.
NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertTime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = #"Glass.aiff";
notifyAlarm.alertBody = #"Staff meeting in 30 minutes";
[app scheduleLocalNotification:notifyAlarm];
}

Resources