How to allocate custom time in UILocalnotification? - ios

- (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.

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

Why my notification is being scheduled 5 hours earlier

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

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

ios. Disable everyday notification if it is already irrelevant

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

Local notifications are repeat on every day and every month

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

Resources