0
votes

I am working on Alarm module in my app. I have develop code which take date and time input from user and set Event on that particular time in my app. Also i store information into my local database to display list of alarm set by user.

Now i want to delete entry from database when particular alarm executed (When UILocalNotification displayed into app i want to call database method to delete that entry from db)

I set Notification by this way

NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
int notificationCount = [preferences integerForKey:@"notification_count"];
notificationCount++;
NSDate* final_date = [calendar dateFromComponents:final_Components];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = final_date;
localNotification.alertBody=titleTextField.text;localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSDictionary* userInfoDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:notificationCount] forKey:@"notification_id"]; localNotification.userInfo = userInfoDic;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[preferences setInteger:notificationCount forKey:@"notification_count"];

I used Delegate didReceiveLocalNotification

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *itemName = [notification.userInfo objectForKey:@"notification_id"];
NSLog(@"userInfo is %@",itemName);
databaseObject.deleteNotification(itemName)
}

My problem is "didReceiveLocalNotification" only call in 2 case

1) When user using app (app in foreground).

2) when notification displayed and user click on notification

But in 3rd case when app is in background mode and notification displayed and if user don't click on notification Or second case is open app directly clicking on app icon or user clear notification at that time didReceiveLocalNotification delegate is not get called..

Is there any way to detect Notification fire in all case or any other method by using i can detect that notification has been fire and then i will execute my delete method.

Any help is appreciated Thank you

1
sorry, that's not how UILocalNotification works. you can't use it to execute code while the app is in the background, you'll have to find another way to achieve this. background execution is a tricky issue on iOS and unfortunately its use is restricted to certain scenariosnburk
@nburk is it possible to check past executed UILocalnotification in applicationWillEnterForeground delegate?Swap-IOS-Android
I run up against this with some scheduling I wanted to do. The best solution seemed to be to do a data push notification, that allows some processing but finding a service that offered scheduled pushes at a good price that you can configure from the app is hard.Gary Riches

1 Answers

0
votes

Yes, you are right, you won't know the information of the notification if your notification fires while your app is suspended/terminated and user didn't tap the notification to launch/active your app.

The only way to do this is to calculate the time every time your app is running and reschedule notifications and update your database.

For millisecond calculation:

NSInteger myMillisecond;  //assume it exists
const NSTimeInterval oneSecondAsMilliseconds = 1000.0;
NSTimeInterval myTimeInterval = myMillisecond/oneSecondAsMilliseconds;
NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimeStamp = [currentDate timeIntervalSince1970];
if (myTimeInterval > currentTimeStamp) {
    //myTimeInterval is a later time than now
}