Getting iPhone notifications to call my delegate when the application is running in the background
I have an enterprise application that I want to keep running, so it can call a webservice and inform the user when there is something they need to do.
So, it now runs in the background, it makes the calls, gets results, but informing the 开发者_JAVA百科user is my problem.
When I fire off a UILocalNotification
the alert doesn't call my UIAlertDelegate
, and I don't see how to set that alert to do that.
Here is my code for how I am doing the notification, and this actually brings up an alert, but I need it to then bring up a View
so they can see the table, but the View
it opens isn't one of the two my application uses.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ has a message for you.", nil), @"FYR"];
localNotif.alertAction = NSLocalizedString(@"Read Msg", nil);
localNotif.soundName = nil;
localNotif.applicationIconBadgeNumber = -1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Your Background Task works", ItemListKey, @"Message from FYR", MessageTitleKey, nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
}
Also, so I tried to have an alert come up that I can set the delegate, so in my controller, where I call the above code I am also calling this:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ShowAlert"
object:nil
userInfo:mydata];
This notification is then picked up and eventually this function is called, and this does work, but the alert isn't visible to the user, I expect because it is in the background.
- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title {
NSLog(@"%@", @"Attemping to show alert");
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title
message:pushmessage
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes",nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
In the main delegate, I have this defined, and the appropriate functions:
@interface FYRViewAppDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate> {
my thought being that if I can get the alert that pops up to call this delegate then it can execute this code:
- (void)alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
alertview--;
if (buttonIndex == 0) {
} else {
[self.window addSubview:[tableController view]];
}
}
So, is there any way to get the UILocalNotification
to use my delegate for the alert?
Or, is there a way to have an alert show up when the application is in the background?
Or, do I need to write two applications, so they communicate with remote notifications, so when the application is in the background one runs and then starts up the main application with a remote notification. I don't like this approach as it seems very messy, but would probably work.
Since the program never stops running
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
is only called at startup, the local notification never calls it and this also is never called:
- (void) application:(UIApplication*)application
didReceiveLocalNotification:(UILocalNotification *)localNotification {
.
I suppose that you don't post notifications when the application is active. Couldn't you simply set a flag when you post a local notification and then check the flag when the application becomes active:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (localNotificationPosted) {
// Do whatever you need to do.
}
}
This will likely be the correct behavior, since the application will become active unless the user dismisses the notification. Even if the user dismisses the notification, showing new messages the next time the app is opened probably isn't a bad thing.
精彩评论