Why my notification is being scheduled 5 hours earlier - ios

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];
}

Related

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

local notifications issue delivering on ipad vs iphone

I have issues on the local notifications for my app. when I test my app for its notifications, on the iphone it is firing once daily but on the ipad 5x time daily. here's my code :
NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];
NSInteger day = [comp day];
NSInteger month = [comp month];
NSInteger year = [comp year];
NSInteger hour = [comp hour];
NSInteger min = [comp minute];
NSInteger sec = [comp second];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: hour];
[components setMinute: min];
[components setSecond: sec];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateToFire;
int i = arc4random() % 2;
if (i == 1) {
localNotification.alertBody = #"Collect your FREE coins!";
} else {
localNotification.alertBody = #"It's time to win";
}
localNotification.alertAction = #"View";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setRepeatInterval: kCFCalendarUnitDay];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(#"local notif = %#", localNotification);
What is my mistake in this code? why it seems that it correctly gives notification on iPhone but not on the iPad?
If in your iPad OS version > 8.0 then you have to take permission from user. Write below code in "didFinishLaunchingWithOptions" (AppDelegate.m)
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

Local notification with a specific day in iOS8

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];

Weekday Local Notification Not Firing

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];

Multiple Notifications Not Firing

I'm scheduling two notifications as shown below. The app is a long-lived app. One local notification is scheduled to run every hour. The other is scheduled to run once per day. Only the second scheduled notification (the hourly notifcation) fires.
- (void)scheduleNotification
{
LogInfo(#"IN scheduleNotification - DELETEYESTERDAY NOTIFICATION SCHEDULED.");
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSDictionary *deleteDict = [NSDictionary dictionaryWithObject:#"DeleteYesterday"
forKey:#"DeleteYesterday"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 45];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.userInfo = deleteDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
and then I schedule this after above:
- (void)scheduleHeartBeat
{
LogInfo(#"IN scheduleHeartBeat - HEARTBEAT NOTIFICATION SCHEDULED.");
UILocalNotification *heartbeat = [[UILocalNotification alloc] init];
NSDictionary *heartbeatDict = [NSDictionary dictionaryWithObject:#"HeartBeat"
forKey:#"HeartBeat"];
heartbeat.userInfo = heartbeatDict;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 50];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
heartbeat.fireDate = dateToFire;
heartbeat.timeZone = [NSTimeZone systemTimeZone];
heartbeat.repeatInterval = NSHourCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:heartbeat];
}
The above are scheduled when the app launches in the viewDidLoad of the main view controller.
- (void)viewDidLoad
{
[self scheduleNotification];
[self scheduleHeartBeat];
[super viewDidLoad];
//OTHER CODE HERE
}
Then in the appdelegate I have the following:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
LogInfo(#"IN didReceiveLocalNotification NOTIFICATION RECEIVED.");
NSString *notificationHeartBeat = nil;
NSString *notificationDeleteYesterday = nil;
application.applicationIconBadgeNumber = 0;
if (notification) {
notificationHeartBeat = [notification.userInfo objectForKey:#"HeartBeat"];
notificationDeleteYesterday = [notification.userInfo objectForKey:#"DeleteYesterday"];
LogInfo(#"IN didReceiveLocalNotification HEARTBEAT NOTIFICATION TYPE: %#", notificationHeartBeat);
LogInfo(#"IN didReceiveLocalNotification DELETEYESTERDAY NOTIFICATION TYPE: %#", notificationDeleteYesterday);
}
if ([notificationHeartBeat isEqualToString:#"HeartBeat"]) {
//CREATE THE HEARTBEAT
LogInfo(#"CREATING THE HEARTBEAT.");
//CALL THE FUNCTIONALITY HERE THAT CREATES HEARTBEAT.
}
if ([notificationDeleteYesterday isEqualToString:#"DeleteYesterday"]) {
//DELETE YESTERDAYS RECORDS
LogInfo(#"DELETING YESTERDAYS RECORDS.");
}
}
The notification that is scheduled last (scheduleHeartBeat) is the only notification that is fired.
Could somebody help me figure out why this is happening?
You have specified your repeat interval to NSDayCalendarUnit. So, your notification will be fire but at next day at specified time.
For testing purpose change your this repeat interval and check your code is working properly.
I have tested. Your code is working properly here.

Resources