Notification in iphone app - ios

I have read many post of notification but somehow i am going wrong somewhere so that's why i post this question. I want to get daily notification at 9.00 am in my app. I get it properly at 9.00 am with no problem what so ever but the difficulty is that i also get same notification at 2.00 am. I try with following code. Can anyone tell me where i am getting wrong. Or is it problem of ios6. Any kind of help will be appreciated. Thank you.
NSString *day =#"9:00 AM";
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:#"hh:mm a"];
//NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
//[dateFormat setTimeZone:gmt];
NSDate *today=[dateFormat dateFromString:day];
NSLog(#"string %# & date %#",day,today);
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
// delObj.QCouter=delObj.QCouter+1;
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
notif = [[cls alloc] init];
notif.fireDate =today;
notif.timeZone = [NSTimeZone systemTimeZone];
NSLog(#"timeZone %# ",[NSTimeZone systemTimeZone]);
notif.alertBody = #"You have a new letter ";
notif.alertAction = NSLocalizedString(#"View", nil);;
notif.soundName = #"Ding3.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[NSUserDefaults standardUserDefaults] setObject:#"1" forKey:#"Status"];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"You have a notifiaction"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
// NSLog(#"userInfo %#",notif.userInfo);
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
[[NSUserDefaults standardUserDefaults] setObject:#"CurrentDay" forKey:#"DayChange"];
}

Hi try following code:-
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 9];
[components setMinute: 0];
[components setSecond: 0];
NSDate *today = [gregorian dateFromComponents: components];
[gregorian release];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
notif = [[cls alloc] init];
notif.fireDate =today;
notif.alertBody = #"You have a new letter ";
notif.alertAction = NSLocalizedString(#"View", nil);;
notif.soundName = #"Ding3.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[NSUserDefaults standardUserDefaults] setObject:#"1" forKey:#"Status"];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"You have a notifiaction"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
// NSLog(#"userInfo %#",notif.userInfo);
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
[[NSUserDefaults standardUserDefaults] setObject:#"CurrentDay" forKey:#"DayChange"];
}

Simulator or real device?
Simulator has a known bug where it generates two "fires" of a single notification. If that's what's happening, try it on a physical device and see if the same behavior occurs. It's annoying, but not an actual problem with your app. (Assuming it's the simulator, of course!)
See this question: iOS – UILocalNotification fired twice for same notification
Edit based on "not in the simulator":
try adding a call to this after you schedule the notification, and see if some other part of your code is slipping in another scheduled item you're not aware of:
- (void) _debug_logExistingToConsole
{
if (LOG) NSLog(#"Notifications set is now: \n");
UIApplication *Ap = [UIApplication sharedApplication];
NSArray* arr = [Ap scheduledLocalNotifications];
if (LOG) NSLog(#"%#", arr);
}

Related

How to set NSLocalNotification to run on monthly or weekly basis for the same time scheduled?

I have my app with data coming from server. I have 2 type of notifications say monthly. Now I will get data say for month with (date-time format) 2-2:30,6-9:00,23-10:00,26-12:00. where 2 denotes date followed by time with colon (:) format.
Now I want to run the notification every month on the same dates and time. I am able to run the multiple notifications. But they do not repeat every month. How to do that? Following is my code for the same.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UIApplication sharedApplication].idleTimerDisabled = YES;
UIUserNotificationSettings *notiSett = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:NULL];
[[UIApplication sharedApplication]registerUserNotificationSettings:notiSett];
[self generateLocalNotificationDaily];
}
-(void)generateLocalNotificationDaily
{
// [[UIApplication sharedApplication] cancelAllLocalNotifications];
[self compareDateDaily];
}
-(void)compareDateDaily
{
NSString *strTime = [Helper getPREF:PREF_AlARM_DAILY_TIME];
NSArray *arrtime = [strTime componentsSeparatedByString:#","];
for(int i=0;i<[arrtime count];i++)
{
NSArray *arrfirstTime = [[NSString stringWithFormat:#"%#",[arrtime objectAtIndex:i]]componentsSeparatedByString:#":"];
int hh = (int)[[arrfirstTime objectAtIndex:0]integerValue];
int mm = (int)[[arrfirstTime objectAtIndex:1]integerValue];
NSDate *now = [NSDate date];
NSTimeInterval secondsPerDay = 1;//24 * 60 * 60;
NSDate *date = [now dateByAddingTimeInterval:secondsPerDay];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
[components setHour:hh];
[components setMinute:mm];
NSDate *todaySpecificDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *currentDate = [NSDate date];
NSString *strCurrentDate = [formatter stringFromDate:currentDate];
NSString *strSystemDate = [formatter stringFromDate:todaySpecificDate];
currentDate = [formatter dateFromString:strCurrentDate];
todaySpecificDate = [formatter dateFromString:strSystemDate];
NSComparisonResult result = [currentDate compare:todaySpecificDate];
NSString *strAlertMessage = [NSString stringWithFormat:#"%#",[Helper getPREF:PREF_AlARM_MESSAGE]];
NSString *strUserName = [Helper getPREF:PREF_NAME];
NSString *strmsg = [strAlertMessage stringByReplacingOccurrencesOfString:#"user_name" withString:strUserName];
NSString *alertMsg = [strmsg stringByReplacingOccurrencesOfString:#"notification_type" withString:#"daily"];
//
switch (result)
{
case NSOrderedAscending:
{
UILocalNotification* localNotificationDaily = [[UILocalNotification alloc] init];
localNotificationDaily.fireDate = todaySpecificDate;
localNotificationDaily.alertBody = alertMsg;
localNotificationDaily.repeatInterval = NSCalendarUnitDay;
localNotificationDaily.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotificationDaily];
NSLog(#"%# is in future from %#", todaySpecificDate, currentDate);
}
break;
case NSOrderedDescending:
NSLog(#"%# is in past from %#", todaySpecificDate, currentDate);
break;
case NSOrderedSame:
NSLog(#"%# is the same as %#", todaySpecificDate, currentDate);
break;
default:
NSLog(#"erorr dates %#, %#", todaySpecificDate, currentDate);
break;
}
}
}
there is a property in uiLocalNotification "repeatInterval". You have to set the repeat interval to month like this
localNotificationMonthly.repeatInterval = NSCalendarUnitMonth;
It will repeat your notification every month depending upon the date and time you set for the notification. For further inquiry please refer to
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/repeatInterval
for week repeat interval would be NSCalendarUnitWeekOfYear

UILocalNotification repeatInterval on 20 days

I'd like to create local notification with custom interval repeat (every 20 days for example).
I know we have NSDayCalendarUnit, kCFCalendarUnitMonth ... but I hope to set repeat interval at 20 days.
I don't want to create a notification for every day.
My real need is to repeat a notification for consecutive 21 days, then don't launch it for 7 days later, then a new 21 days with notification and 7 days without ... etc.
I should schedule all these days even if application is inactive.
To do this I decide to create a 21 notifications since a fire date with repeatInterval = 28days (here is the problem)
Try this, you can change the intervall by selecting setDay, setMonth, .... :
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:3];
NSDate *date3Days = [calendar dateByAddingComponents:components
toDate:[NSDate date]
options:0];
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
if ( oldNotifications ) {
[app cancelAllLocalNotifications];
app.applicationIconBadgeNumber = 0;
}
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
notifyAlarm.fireDate = date3Days;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.alertBody = NSLocalizedString( #"Push message", #"");
notifyAlarm.soundName = #"sound.wav";
[app scheduleLocalNotification:notifyAlarm];
}
If you want to set a specific time after which the UILocalNotifications should appear you can create a method of the above solution and loop over an array which indicates the days you like to show a notification:
NSArray *arrayNumbers = #[#5, #7, #14, #21, #30, #60, #90, #120];
NSDictionary *dictNotifications =
[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefAppStarts];
for ( NSNumber *bla in arrayNumbers ){
NSString *strKey = [NSString stringWithFormat:#"%#%#", kUserDefAppStarts, bla];
NSDictionary *dict = dictNotifications[strKey];
NSString *strMessageQuestion = dict[kKeyMessage];
[self createNotificationWithNumberOfDays:[bla integerValue]
andMessage:strMessageQuestion
userInfo:dict];
}
And here is the method you have to call
+ (void)createNotificationWithNumberOfDays:(int)days
andMessage:(NSString *)message
userInfo:(NSDictionary *)dict{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
if( notifyAlarm ){
[notifyAlarm setFireDate:dateAlert];
[notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
[notifyAlarm setSoundName:#"soundname"];
[notifyAlarm setAlertBody:message];
[notifyAlarm setUserInfo:dict];
[app scheduleLocalNotification:notifyAlarm];
}
}
If you wanted to schedule a UILocalNotification for 20 days in the future from the current time you would do this:
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
int desiredAmountOfMonths = 4;
for (int month = 0; month < desiredAmountOfMonths; month++)
{
dateComp.month = month;
dateComp.day = 20;
NSDate *fireDate = [currentCalendar dateByAddingComponents:dateComp toDate:[NSDate date] options:NSCalendarMatchNextTimePreservingSmallerUnits];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
}
You will need to modify the UILocalNotification for what message, sound, etc and then fire the notification once done customizing.

How to set multiple UILocalNotifications

In my app I am using UILocalNotifications. I want to set notifications for different days of the week.For that, I have different dates in an array. But I am getting wrong results. Is there anything wrong with my code. My code is
for(int counter=0 ;counter<[daysArray count]; counter++)
{
int day = [[daysArray objectAtIndex:counter] intValue];
NSDate *specificDate = [self getDateOfSpecificDay:day];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = specificDate;
localNotification.repeatInterval = NSWeekdayCalendarUnit;
localNotification.soundName = sound;
localNotification.alertBody = [NSString stringWithFormat:#"%#",specificDate];
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(#"%#",localNotification);
}
we have done something similiar to your question but i dont know it will help you or not..just try this **NSWeekdayCalendarUnit**i suggest you just refer my answer.
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit|NSWeekCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
NSArray *dobArray = [NSArray arrayWithObjects:#"02-08-2014", #"10-08-2014", #"14-08-2014", #"15-08-2014", #"16-08-2014", #"17-08-2014", #"18-08-2014", #"22-08-2014", #"28-08-2014", #"29-08-2014",nil];
NSArray *messagesArray = [NSArray arrayWithObjects:#"Literacy And Numeracy workshop for parents and children", #"raksha Bandhan", #"Tri Colour Dat(School celebration)", #"Independence day (School Holiday)", #"PTM/Book Exhibition", #"Janmastami", #"Parsi New Year- School Holiday", #"Clown Day", #"Eco Friendly Ganesha-School Holiday", #"Ganesh Chaturthi-School Holiday",nil];
for (NSString *dobStr in dobArray)
{
NSArray *components = [dobStr componentsSeparatedByString:#"-"];
if(components.count>2) {
NSInteger aDay = [[components objectAtIndex:0] integerValue];
NSInteger aMonth = [[components objectAtIndex:1] integerValue];
// NSInteger aYear = [[components objectAtIndex:2] integerValue];
if(aDay == [dateComponent day] && aMonth == [dateComponent month]) { // dob is here
[dateComponent setDay:aDay];
[dateComponent setMonth:aMonth];
[dateComponent setYear:[dateComponent year]];
[dateComponent setHour:16];
[dateComponent setMinute:54];
UIDatePicker *dp = [[UIDatePicker alloc] init];
[dp setDate:[gregCalendar dateFromComponents: dateComponent]];
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSInteger index = [dobArray indexOfObject:dobStr];
// [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:[messagesArray objectAtIndex:index]];
[notification setFireDate:dp.date];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
return YES;
}
}
}
I Think here you are misusing the line localNotification.repeatInterval = NSWeekdayCalendarUnit;
it will let your notification repeat every day of week that is creating problem in your case. Please remove this line and try again it will work.

Store multiple UILocalNotification in UserInfo

this is how i setting my local notification, all work well.
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:[_datePicker date]];
NSLog(#"%#",[_datePicker date]);
NSDate *fireDate = [gregorianCalendar dateFromComponents:components];
NSLog(#"Fire date : %#",fireDate);
// check if the time is already passed
if ([fireDate compare:[NSDate date]] == NSOrderedAscending)
{
// if it does add 1 day
components.day = components.day+1;
fireDate = [gregorianCalendar dateFromComponents:components];
}
NSLog(#"Fire date : %#",fireDate);
for (int i = 0; i < 7; i++) {
NSDate *scheduleDate = [fireDate dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
if (componentsForEachDay.weekday != 7) { // To skip Saturday
localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = #"test";
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.applicationIconBadgeNumber = 0;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"%i",i] forKey:[NSString stringWithFormat:#"bracletNotif%i",i]];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
i always get only the last object in userInfo.. how can i store all the scheduleLocalNotification in the userInfo for later cancel?
how does its work? before every scheduleLocalNotification i need so save an object and key? or can i save them all at one?
because i already tried to do that, i got all the userInfo objects but could not delete them :/
SOS please.
If you are trying to cancel all braclet notifications instead of giving them all a different key/value you could do this instead:
localNotification.userInfo = #{#"category": #"bracelet"};
To cancel:
[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
NSDictionary *userInfo;
if((userInfo = [notification userInfo])){
NSString *categoryValue;
if((categoryValue = [userInfo objectForKey:#"category"]) && [categoryValue isEqualToString:#"bracelet"]){
[[UIApplication sharedApplication] cancelLocalNotification: notification];
}
}
}];
What we are doing here is grouping all notifications into the category: bracelet. I have chosen the key category due to future changes in the sdk. We could schedule more notifications e.g. category: neckless
Then when required we loop through all the UILocalNotifications and scan for the category key in the notifications userInfo, in the example above if the keys value equals bracelet, then we cancel the notification.
if((categoryValue = [userInfo objectForKey:#"category"]) && [categoryValue isEqualToString:#"bracelet"]){
This line of code is checking whether a key of category exists (i.e. not nil) whilst assigning it to the variable categoryValue then the if statement is immediately checking if the categoryValue is equal to bracelet
It could have been rewritten as:
NSString *categoryValue = [userInfo objectForKey:#"category"];
if(categoryValue && [categoryValue isEqualToString:#"bracelet"]){

IOS CancelNotificaion on particular date when the app is in Background

I am working on a application that reminds about the expiry date. I have implemented the same using UILocalNotification with repeat Interval (NSMonthCalendarUnit, NSDayCalendarUnit,NSDayCalendarUnit). For example I have the fire date on 01-01-2012 and the repeat interval is NSDayCalendarUnit and the end date is 12-12-2012, is it possbile to cancelLocalNotification: on expiry.
here is the code:-
- (void) scheduleNotificationOn:(NSDate*) fireDate
text:(NSString*) alertText
action:(NSString*) alertAction
sound:(NSString*) soundfileName
launchImage:(NSString*) launchImage
andInfo:(NSDictionary*) userInfo
{
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
txtExpiryDate.text, #"ExpiryDate",
txtRegNo.text , #"RegNo",
nil];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.userInfo = userInfo;
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
NSLog(#"Repeat Type:%#",txtRepeat.text);
if([txtRepeat.text isEqualToString:#"Every Week"])
{
NSLog(#"Every Week");
localNotification.repeatInterval = 256;
}
else if([txtRepeat.text isEqualToString:#"Every Month"])
{
NSLog(#"Every Month");
localNotification.repeatInterval = NSMonthCalendarUnit;
}
else if([txtRepeat.text isEqualToString:#"Every Day"])
{
NSLog(#"Every Day");
localNotification.repeatInterval = NSDayCalendarUnit;
}
if(soundfileName == nil)
{
localNotification.soundName = UILocalNotificationDefaultSoundName;
}
else
{
localNotification.soundName = soundfileName;
}
NSLog(#"appDelegate.BadgeNumber:%d",appDelegate.BadgeNumber);
localNotification.alertLaunchImage = launchImage;
appDelegate.BadgeNumber = appDelegate.BadgeNumber + 1;
localNotification.applicationIconBadgeNumber = appDelegate.BadgeNumber;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
I have worked by comparing current date to expiry date. But this work only if the app is in foreground and i cannot cancelnotification not background for a particular date. Please find the below code for the same:-
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
BadgeNumber = 0;
application.applicationIconBadgeNumber = BadgeNumber;
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDateFormatter *formatter =[[[NSDateFormatter alloc]init] autorelease];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSLog(#"localNotifications Count %d",localNotifications.count);
for (UILocalNotification *notify in localNotifications)
{
//notify.applicationIconBadgeNumber = 0;
NSString *ExpiryDateString = [notify.userInfo objectForKey:#"ExpiryDate"];
NSDate *ExpiryDate = [formatter dateFromString:ExpiryDateString];
NSDate * NextFireDate = nil;
NSLog(#"Expiry Date:%#",ExpiryDateString);
if(notify.repeatInterval == NSDayCalendarUnit)
{
NSLog(#"Repeat Every Day");
NextFireDate = [[NSDate date] dateByAddingDays:1];
NSLog(#"Next FireDate: %#",[formatter stringFromDate:NextFireDate]);
}
if(notify.repeatInterval == NSWeekCalendarUnit)
{
NSLog(#"Repeat Every Day");
NextFireDate = [[NSDate date] addTimeInterval:D_WEEK];
NSLog(#"Next FireDate: %#",[formatter stringFromDate:NextFireDate]);
}
if(notify.repeatInterval == NSMonthCalendarUnit)
{
NSLog(#"Repeat Every Day");
//NextFireDate = [[NSDate date] addTimeInterval:D_Month];
NextFireDate = [self CalculateExipiryDateForMonth];
NSLog(#"Next FireDate: %#",[formatter stringFromDate:NextFireDate]);
}
NSComparisonResult result = [NextFireDate compare:ExpiryDate];
NSLog(#"NSComparisonResult:%d",result);
if(result == NSOrderedDescending)
{
NSLog(#"Cancell......... Notification");
NSLog(#"notify :::%#",notify);
}
else
{
NSLog(#"Re-Schedule Notification");
BadgeNumber = BadgeNumber + 1;
notify.applicationIconBadgeNumber = BadgeNumber;
NSLog(#"BadgeNumber:%d",BadgeNumber);
[[UIApplication sharedApplication] scheduleLocalNotification:notify];
}
}
}
-(NSDate*) CalculateExipiryDateForMonth
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 1;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
[components release];
NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nextMonth];
NSDate *expiryDay = [gregorian dateFromComponents:nextMonthComponents];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSDate *NewExpiry = [gregorian dateByAddingComponents:dayComponent toDate:expiryDay options:0];
[gregorian release];
[dayComponent release];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSLog(#"Next Exipiry Date -----:%#",[formatter stringFromDate:NewExpiry]);
return NewExpiry;
}
In short, no, you cannot cancel a UILocalNotification while your app is running in the background.
**Apple RESPONSE:-**
I'm responding to your question about UILocalNotification.
At this time UILocalNotification does not have a way to specify an expiry
date or number of repetitions before the notification is automatically
canceled.
The closest you can get today is to schedule up to 64 individual
notifications instead of using the repeat interval.
But you're correct; if the user doesn't ever bring your app to the
foreground, it won't have the opportunity to cancel or reschedule local
notifications.
I highly recommend that you file an enhancement request at <
https://developer.apple.com/bugreporter> asking for this functionality in a
future release of iOS.
You also asked about what happens to local notifications when the user
removes your app from the device. iOS stores the local notifications so
that they are still scheduled if the user deletes and then reinstalls the
app.
When your app runs, it can check the scheduledLocalNotifications property
of UIApplication and remove any notifications that are no longer relevant.
Best regards,
--gc
Garth Cummings
Apple Developer Technical Support

Resources