Reputation: 4109
I schedule a Notification, and give it 60 minutes warning before it should display an alert message...
As soon as I add the notification the method in my App Delegate is called:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
How can I know that it will show up in the background as an alert? Is there any other Delegate method I need to override or use in order to ensure that given a scheduled Alert with a 60 Minute Interval...
- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
//NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentDateComponents = [calendar components:( NSWeekdayCalendarUnit |
NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item];
NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);
NSLog(@"[currentDateComponents minute]: %i", [currentDateComponents minute]);
NSLog(@"[currentDateComponents hour]: %i", [currentDateComponents hour]);
NSLog(@"[currentDateComponents day]: %i", [currentDateComponents day]);
NSLog(@"[currentDateComponents week]: %i", [currentDateComponents week]);
NSLog(@"[currentDateComponents month]: %i", [currentDateComponents month]);
NSLog(@"[currentDateComponents year]: %i", [currentDateComponents year]);
[dateComps setDay: [currentDateComponents day]];
[dateComps setMonth:[currentDateComponents month]];
[dateComps setYear:[currentDateComponents year]];
[dateComps setHour:[currentDateComponents hour]];
[dateComps setMinute:[currentDateComponents minute]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@",
streetAddress,
stringOfWhenAuctionIsOn];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress
forKey:idOfStreetAlert];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Upvotes: 0
Views: 1442
Reputation: 10080
If the notification shows up right away there probably is something wrong with your fireDate property. It looks like you are trying to verify that the date sent is correct but you should also verify that that fireDate is what you expect.
Also, you could perhaps do
localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];
to achieve the same effect and don't have to mess around with NSDateComponents
. I have not tested this but it might work.
As for what happens when the timer fires and your app is not running. If the app has not been terminated application:didReceiveLocalNotification:
will get called. If on the other side your app has been terminated you need to check in application:didFinishLaunchingWithOptions:
as pointed out by @Ishu. I usually just pass the notification to a common method to avoid duplicating the code.
Upvotes: 1
Reputation: 12787
No delegate method for background, you need to write code in didFinishLaunchingWithOptions,
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
//your code
NSLog(@"Recieved Notification %@",localNotif);
}
it create your logic when app get notification from background.Dont worry app get notification ones you set the notification.
Upvotes: 0