Reputation: 891
Well the title is self explained. I want to create an App that can manage programmed local notifications when App is in background mode. Notifications works smoothly but I want to fire a custom method when the alert is fired.
Is it possible? Thank you.
Upvotes: 9
Views: 6267
Reputation: 303
Yes it can be done. You can do somethng like this:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:17.0 target:self selector:@selector(makeNotificationRequest:) userInfo:nil repeats:YES];
}
-(void)makeNotificationRequest:(NSTimer *)timer
{
CLLocation *location = [[AppHelper appDelegate] mLatestLocation];
NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
#ifdef _DEBUG
[paramDic setValue:[NSString stringWithFormat:@"77.586"] forKey:@"Lat"];
[paramDic setValue:[NSString stringWithFormat:@"12.994"] forKey:@"long"];
#else
[paramDic setValue:[NSString stringWithFormat:@"%f",location.coordinate.latitude] forKey:@"Lat"];
[paramDic setValue:[NSString stringWithFormat:@"%f",location.coordinate.longitude] forKey:@"long"];
#endif
WNetwork *mNetwork = [[WNetwork alloc] init];
[mNetwork makeRequsetWithURL:URL_Showbeeps type:JBJsonParser paramDictionary:paramDic delegate:self];
[mNetwork autorelease];
NSLog(@"URL HIT%@",paramDic);
[paramDic autorelease];
}
And to customize your action on alert you can use this:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
;
}
}
Upvotes: 6
Reputation: 1663
No ..no custom methods are defined while app is running in background..Once an notification is fired we can;t change the alert message also. But nice when we show the alert message then by clicking the yes button to the alert take you to the one method called app is Running in Background which is in AppDelegate.m file
Upvotes: 1
Reputation: 7946
If the app were in the foreground then you would want the - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
method of UIApplicationDelegate
, however the documentation suggests that this will not be called when the app is in the background.
You can get a list of local notifications by calling scheduledLocalNotifications
on your UIApplication
instance - you can then poll these for their times and schedule a function to call at that time in your background mode. This won't necessarily 100% match up with when the Local Notification fires though, but I think it's as close as the App Store guidelines will let you get.
You can also present your own Local Notifications when you are in background mode by calling the presentLocalNotificationNow:
method:
so you could work around this by just presenting your own notifications and not scheduling them for the OS to deliver.
If you are trying to access Local Notifications from other applications then I don't think this is allowed.
Upvotes: 1