enable uilocalnotification
Need to enable this notification schedules daily, I'm not getting, I created 3 different methods for each hour worked but it is not.
8:00 AM
12:00 AM
4:00 PM
-(void)Hour_08 {
NSString *str = [NSString stringWithFormat: #"%# 08:00", self.DayNow];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate *date = [dateFormatter dateFromString: str];
UILocalNotification *OitoHorasNotification = [[UILocalNotification alloc]init];
[OitoHorasNotification setFireDate:date];
OitoHorasNotification.repeatInterval = NSDayCalendarUnit;
[OitoHorasNotification setAlertBody:#"is now 08:00"];
[[UIApplication sharedApplication]scheduleLocalNotification:OitoHorasNotification];
}
Have you taken a look at this site as it suggests you use NSHourCalendarUnit instead of NSDayCalendarUnit which would be the case if you performing notifications after whole days. Or Maybe you are performing Notifications for whole days and therefore your code would be correct I would of miss read you question
Related
I am getting current date by using this
NSDate *currDate;
currDate = [NSDate date];
But If user opens my app from other country also the currDate should be Indian time only.
How can I do this?
Check if this helps, there could be multiple ways of approaching this - but this is readable -
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
NSString *indianTimeZone = [dateFormatter stringFromDate:currentDate];
NSLog(#"%#", indianTimeZone);
I learned about "Asia/Kolkata" by logging -
NSLog(#"%#", [NSTimeZone knownTimeZoneNames]);
Also, checkout this answer, I like this approach.
I want to set a UILocalNotification to go off at a certain time. The app deals with restaurant bookings. If a booking is made and the appointment is greater than 2 hours from now, then I want to show the notification 2 hours before it. If it is greater than 1 hour then I was to show it 1 hour before hand, else I want to show it 15 mins before hand. The dificulty Im having is that my UILocalNotification fire time is wrong by + 1 hour. Below is the code used:
[self setLocalNotificationWithAlertBody:#"Alert Body goes here" AndWithBookingDateString:#"2015-09-07 19:45:00"];//the booking time
-(void)setLocalNotificationWithAlertBody:(NSString *)body AndWithBookingDateString:(NSString *)dateString{
NSLog(#"Date String %#",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Date from String %#",[dateFormatter dateFromString:dateString]);
[self setLocalNotificationWithAlertBody:body AndWithBookingDate:dateFromString];
}
-(void)setLocalNotificationWithAlertBody: (NSString *) body AndWithBookingDate: (NSDate *)date{
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit
fromDate:currentDate
toDate:date
options:0];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSInteger hour = [components hour];
if (components.hour>=2) {//remind the user 2 hours before hand if booking is made greater than 2 hours
NSTimeInterval secondsPerHour = (60 * 60)*2;//two hours before appointment
NSDate *givenDate = date; // what you have already
NSDate *earlierDate = [givenDate dateByAddingTimeInterval:-secondsPerHour];
NSLog(#"\n\n Greater than 2 hours %# \n\n",earlierDate);
localNotification.fireDate=earlierDate;
localNotification.alertBody = [NSString stringWithFormat:#"%# %#",body,[self getDeviceShortDateFromString:[NSString stringWithFormat:#"%#",date] WithFormat:#"yyyy-MM-dd HH:mm:ss ZZZZZ"]];
}
else if (components.hour>1) {
NSTimeInterval secondsPerHour = 60 * 60;
NSDate *givenDate = date; // what you have already
NSDate *earlierDate = [givenDate dateByAddingTimeInterval:-secondsPerHour];
NSLog(#"\n\n Greater than an hour %# \n\n",earlierDate);
localNotification.fireDate=earlierDate;
localNotification.alertBody = [NSString stringWithFormat:#"%# %#",body,[self getDeviceShortDateFromString:[NSString stringWithFormat:#"%#",date] WithFormat:#"yyyy-MM-dd HH:mm:ss ZZZZZ"]];
}
else{
NSTimeInterval secondsPer15mins = 15 * 60;
NSDate *givenDate = date; // what you have already
NSDate *earlierDate = [givenDate dateByAddingTimeInterval:-secondsPer15mins];
NSLog(#"\n\n Less than one hour %# \n",earlierDate);
localNotification.fireDate=earlierDate;
localNotification.alertBody = [NSString stringWithFormat:#"%# %#",body,[self getDeviceShortDateFromString:[NSString stringWithFormat:#"%#",date] WithFormat:#"yyyy-MM-dd HH:mm:ss ZZZZZ"]];
}
localNotification.userInfo = #{#"key" : body};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(#"Fire date %#",localNotification.fireDate);
NSLog(#"Notification--->: %#", localNotification);
}
-(NSString *)getDeviceShortDateFromString: (NSString *) date WithFormat: (NSString *)format{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:format];
NSDate * tempDate =[dateFormatter dateFromString: date];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"MMM yyyy"];
if (tempDate == nil) {
return #" ";
}
return [dateFormatter2 stringFromDate:tempDate];
}
The fire date is logging as:
Fire date 2015-09-07 17:45:00 +0000
But the local notification is wrong:
Notification--->: <UIConcreteLocalNotification: 0x174178300>{fire date = Monday 7 September 2015 18:45:00 Irish Summer Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday 7 September 2015 18:45:00 Irish Summer Time, user info = {
key = "Alert Body Goes here";
}}
The appointment was booked greater than 2 hours so the notification should be going off at 5:45 but it doesnt go off until 6:45. Any help is greatly appreciated.
"Help me StackOverflow, you're my only hope" :)
For scaleable apps that rely on location specific results, ensure that your location specific properties always point to the users relative location. In your case, you just overlooked a simple setting:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
For you, this is +1 hour, but for others it will vary some will be ahead and some will be behind, because your basing the fireDate on GMT's time.
I would simply alter this to be [NSTimeZone systemTimeZone]
I asked a question not too long ago about timezone and I was using EST. Users suggested me to use EDT. I want to know why I should use one or the other because they both print the same time for me. Here is the code to better illustrate what I mean.
NSDate *today = [NSDate date];
NSDateFormatter *edtDf = [[NSDateFormatter alloc] init];
[edtDf setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EDT"]];
[edtDf setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate = [edtDf stringFromDate:today];
NSLog(#"The EDT is %#", stringDate);
NSDate *today1 = [NSDate date];
NSDateFormatter *estDf = [[NSDateFormatter alloc] init];
[estDf setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EST"]];
[estDf setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate1 = [estDf stringFromDate:today1];
NSLog(#"The EST is %#", stringDate1);
They may print different things depending on the time of year (since time of year determines whether Daylight Saving Time is active).
Don't use EST or EDT. Use US/Eastern or America/New_York:
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"US/Eastern"];
// or
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"America/New_York"];
These time zones adjust for Daylight Saving Time at the correct times of the year.
I absolutely hate NSDate. Just when I think I have my head wrapped around it, DST either starts or ends, and wrecks everything I am working on.
I am parsing an XML file which in the pubDate has:
Fri, 1 Aug 2014 20:15:00 -0500
The event occurs at 8:15 PM in the Central time zone. This would normally be -0600, but due to taking place during DST, it becomes -0500.
Anyways, since this is just a string of text in the XML, I use NSDateFormatter to format it to an NSDate, and then store it as a property of a class I created:
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *dateofarticle = [dateFormatter stringFromDate:articleDate];
NSDate *date = [dateFormatter dateFromString:dateofarticle];
I have this date appear in the detailTextLabel of a TableView cell, and it shows perfectly as 8:15 PM using:
NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
[formatter3 setTimeStyle:NSDateFormatterShortStyle];
[formatter3 setDateStyle:NSDateFormatterShortStyle];
NSString *detailstext = [formatter3 stringFromDate:entry.articleDate];
cell.detailLabel.text = [NSString stringWithFormat:#"%# - Tap for more details.", detailstext];
So far, all is well. I have the time of 8:15 PM from the XML, and it shows 8:15 PM in the TableView. It is when setting a reminder that things get screwy.
I make the property self.theDate in my reminder class equal the same articleDate I have saved from the first part of all this, and then run this code:
NSDate *newDate = [self.thedate dateByAddingTimeInterval:-60*15];
NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
[formatter3 setTimeStyle:NSDateFormatterShortStyle];
[formatter3 setDateStyle:NSDateFormatterShortStyle];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.timeZone = nil;
notification.fireDate = newDate;
From all this, I would expect it to fire at 8:00 PM on August 1st, but when I schedule notification and then change the time on my phone to this date, it fires one hour too late, at 9:00 PM.
Any thoughts?
This question already has answers here:
Schedule number of Local Notifications
(6 answers)
Closed 7 years ago.
I am developing a calendar app which consists of multiple events(e.g 500 events) which consists of birthdays.I am downloading events form web service. I want to give user the alert on each birthdate at a specific time sat 10:00 AM on the birthday. I am using local notification for scheduling a alert but I am stuck as iOS allows only 64 notifications per app and I have multiple birthdays. I don't know how to schedule more notifications once the app crosses the limit. Please suggest how would I solve this problem. below is my code to schedule notifications
- (void)scheduleNotification:(NSMutableArray *)datesArray withMessage:(NSMutableArray *)messagesArray NotificationID:(NSString *)notificationID
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
[formatter1 setDateStyle:NSDateFormatterMediumStyle];
[formatter1 setDateFormat:#"dd/MM/yyyy"];
// NSDate *myTime = [formatter dateFromString:#"06:10 PM"];
NSDate *myTime = [formatter dateFromString:fireTime];
for (int i = 0; i < [datesArray count]; i++)
{
NSDate *myDate = [formatter1 dateFromString:[datesArray objectAtIndex:i]];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:myDate];
NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myTime];
NSDateComponents * newComponents = [[NSDateComponents alloc]init];
NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
int day = [dateComponents day];
int month = [dateComponents month];
[newCompoents setDay:[dateComponents day]];
[newCompoents setMonth:[dateComponents month]];
if (day >= [todayComponents day] && month >= [todayComponents month]) {
NSLog(#"day = %d, month = %d", day, month);
[newComponents setYear:[todayComponents year]];
} else {
NSLog(#"%d, %d", day, month);
[newComponents setYear:[todayComponents year]+1];
}
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
NSDate *combDate = [calendar dateFromComponents: newComponents];
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = combDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
NSString *message = [#"Wish" stringByAppendingFormat:#" %#%#", [messagesArray objectAtIndex:i],#" On His Birthday"];
localNotif.alertBody = message;
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}
correct me if I am making any mistakes and suggest how to schedule more notifications once 64 limit is crossed.
I have a similar problem and a few additional options that may help you, though they all have flaws that means correct behavior is not guaranteed.
Keep in mind that overlapping birthdays could be useful here. If two people end up having a birthday on the same day, cancel and reschedule with both of their info associated. You could find other intervals where repetition occurs if you're OK w/ a generic message.
Obviously, encouring your user to press the notification to open the app would help (possible with your ad revenue too). You could try a nice message for your last one.
It's hacky, but you could use geofencing (but probably not remote notifications) as described here: Can push notifications be used to run code without notifying user? . If you add a feature that uses it, you might even be able to get it approved.
I've considered passing a custom calendar as well, but NSCalendar is toll free bridged to CFCalendarRef and the story seems to be that I'm not going to have luck trying to dig into that (and getting it approved at least). I would be happy to be told I'm wrong.