I want display local notification on my app in a specific day, for example, I want the notification is displayed all the 1 of the month.
I have this code, how can I customize it ?
//LOcal Notification
NSDate *today = [NSDate new];
NSCalendar *gregorian =
[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [NSDateComponents new];
[offsetComponents setDay:???];
[offsetComponents setHour:10];
[offsetComponents setMinute:0];
NSDate *thedate = [gregorian dateByAddingComponents:offsetComponents
toDate:today options:0];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = thedate;
localNotification.alertBody = #"Hey, il serait temps de faire un point non ? \ue104";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
//END
There is too a depreciated error with ios8 on the gregorian calendar, how can I resolve it ?
//assuming you want to fire the notifiaction on 1st of every month.
NSInteger desiredWeekday = 1;// it specifies on which day of month
NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSWeekdayCalendarUnit];
NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSInteger currentWeekday = dateComponents.weekday;
NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek;
NSDateComponents *daysComponents = [[NSDateComponents alloc] init];
daysComponents.day = differenceDays;
NSDate *resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:[NSDate date] options:0];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = resultDate;
localNotification.alertBody = #"Hey, il serait temps de faire un point non ? \ue104";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSMonthCalendarUnit;// repeat notifiaction after a month
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Related
I am making an app where I have to schedule local notifications for all the time stamps that is stored in my database when the app will be installed for the first time.
The notifications are being scheduled but when the alarm rings, I realize it is ringing at a time that is exactly 5 hours delayed than the current time. Imagine, I have 3 notifications to go off today- one at 2:11 pm, another at 3:18 pm and another at 8:15 pm. Now, imagine my current time is 7:11pm. I will have a notification go off. which is exactly 5 hours after my first timestamp. Then again it is go off at 8:18 pm.
The following is my code. Can anyone please help.
-(void) setLocalNotification {
TimeCalculationLogic *timeManager = [[TimeCalculationLogic alloc]init];
NSMutableArray *allTimeStamps = [timeManager getAllTimeStamps];
NSDate *currentTime;
for(beginningTime *time in allTimeStamps){
NSDate *currentDate = [timeManager getDateFromString:time.activeDate];
currentTime = [timeManager getDateTimestampFromString:time.first];
[self getComponentToScheduleNotificationFromDate:currentDate andTime:currentTime];
currentTime = [timeManager getDateTimestampFromString:time.second];
[self getComponentToScheduleNotificationFromDate:currentDate andTime:currentTime];
currentTime = [timeManager getDateTimestampFromString:time.third];
[self getComponentToScheduleNotificationFromDate:currentDate andTime:currentTime];
}
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
-(void)getComponentToScheduleNotificationFromDate:(NSDate*)date andTime:(NSDate*)timestamp{
self.schedulerComponent = [[NSDateComponents alloc] init];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
NSDateComponents *components;
components = [calender components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[self.schedulerComponent setYear:year];
[self.schedulerComponent setMonth:month];
[self.schedulerComponent setDay:day];
components = [calender components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:timestamp];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
[self.schedulerComponent setHour:hour];
[self.schedulerComponent setMinute:minute];
[self.schedulerComponent setSecond:second];
[self.schedulerComponent setTimeZone:[NSTimeZone systemTimeZone]];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
NSDate *setTime = [cal dateFromComponents:self.schedulerComponent];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = setTime;
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"It's time Again!";
localNotif.alertAction = #"View";
localNotif.soundName = #"ajan.caf";
localNotif.applicationIconBadgeNumber = 1;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
I'm building an application that allows the user to have a local notification to repeat every weekday, because apple doesn't have a repeat interval for WeekDays I needed to create a local notification for every week day. I made a for loop looping 5 times the number of days in a work week. However it doesn't seem to be firing. But when I got to delete the notifications I see all five, the just wont fire. This is what my code looks like.
NSDate *today = [NSDate date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = _alarmTime;
localNotification.alertBody = #"Wake up or pay-up!";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertAction = #"Wake up or pay-up!";
localNotification.soundName = #"sound_ring.caf";
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = #"Everyday";
break;
case 2:
//Attempting to create 5 local notifications with different days but same time.
for (int i = 2; i <= 6; i++){
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(#"%D",i);
}
If anyone has any ideas please let me know! Thanks in advance.
Update:
NSDate *startDate = [gregCalendar dateFromComponents:dateComponent];
[dateComponent setMonth:0];
[dateComponent setDay:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setYear:0];
NSDate *fireDate = [gregCalendar dateByAddingComponents:dateComponent toDate:startDate options:0];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(#"%D",i);
Okay I was finaly able to get local notifications to repeat every weekday, I added [setHour] and [setMinute] as well as changed the int in the for loop to a NSUInteger. Here is my code below.
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = #"Everyday";
break;
case 2:
for (NSUInteger i = 2; i <= 6; i++) {
NSDateComponents *components = [gregCalendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:_alarmTime];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponent setMinute:minute];
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setHour:hour];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = fireDate;
localNotification2.alertBody = #"Wake up or pay-up!";
localNotification2.timeZone = [NSTimeZone systemTimeZone];
localNotification2.alertAction = #"Wake up or pay-up!";
localNotification2.soundName = #"sound_ring.caf";
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(#"%lu",(unsigned long)i);
}
They don't fire because the date you give them is invalid.
Here's how I generated the date for X number of days in the future:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:0];
[components setDay:X];
[components setYear:0];
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];
I want to remind user to take photo everyday. I use code for every day local push notificatons
UILocalNotification *everyDayNotification = [[UILocalNotification alloc] init];
everyDayNotification.repeatInterval = NSDayCalendarUnit;
NSDate *currentDate = [[NSDate alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
int hour = [hours_string intValue];
int minutes = [minutes_string intValue];
[components setHour:hour];
[components setMinute:minutes];
NSDate *today10am = [calendar dateFromComponents:components];
everyDayNotification.fireDate = today10am;
everyDayNotification.timeZone = [NSTimeZone defaultTimeZone];
everyDayNotification.alertBody = #"It us time";
everyDayNotification.alertAction = #"Action";
everyDayNotification.soundName = UILocalNotificationDefaultSoundName;
everyDayNotification.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"EveryDay"
forKey:#"RemindNotificaion"];
everyDayNotification.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:everyDayNotification];
But if the user has made the photo before it is time to notice that. I want to skip the notification for today. Is that possible?
I would suggest making an individual UILocalNotification for each day and storing them, perhaps in an array in NSUserDefaults. Then you can cancel a notification with the following:
[[UIApplication sharedApplication] cancelLocalNotification:notficiationInstance];
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
I wrote an app that allows the user to enter a date on a date picker, and then when they tap a button it will schedule a local notification. The only issue is, the notification fires right when I tap the button. Any help is much appreciated! Here is my code:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [self.datePicker date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-3];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = targetDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
You need to use [NSCalendar dateByAddingComponents:toDate:options:] instead:
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [self.datePicker date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-3];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
[dateComponents release];
...
If you're looking for complete code on how to schedule a notification a bit later (like say 3 seconds) here's the complete code :
NOTE : If you're inside the app you won't see a message box at the top of the screen, that might have to be handled through the UIApplication delegate.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setSecond: 3 ];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
localNotification.fireDate = targetDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Notified";
localNotification.alertAction = #"Show";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];