I'm trying to fire a local notification on repeated intervals, but I want it to call another method every time a notification is sent. Is that possible?
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
//Repeat every 60 seconds
NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:60];
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Notification Sent!"; //send notification
localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
//call method??
I want to call this method:
-(void) callAfterSixtySecond
{
//do something
}
Yes and no. If the app is active when the notification fires then application:didReceiveLocalNotification:
will be called in app delegate and you could use this to call the method. However, if the app is in the background then this method won't get called unless the user launches the app by tapping on the notification in Notification Center, and so it won't be when the notification first fires.
One way you could do this is to schedule the method separately with something like the following:
[self performSelector:@selector(callAfterSixtySecond) withObject:nil afterDelay:60];
This will work as long as the app still has processing time when the method should run. If the user has closed the app this it generally only has around 10s of processing time but in same cases this can be increased by starting a background task (see beginBackgroundTaskWithName:expirationHandler:
in UIApplication
).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments