当应用程序在后台运行时获取 iPhone 通知来呼叫我的代表

发布于 2024-11-19 00:25:52 字数 2925 浏览 4 评论 0原文

我有一个想要继续运行的企业应用程序,因此它可以调用 Web 服务并在用户需要执行某些操作时通知用户。

所以,它现在在后台运行,它进行调用,获取结果,但通知用户是我的问题。

当我触发 UILocalNotification 时,警报不会调用我的 UIAlertDelegate,而且我不知道如何设置该警报来执行此操作。

这是我如何执行通知的代码,这实际上会弹出一个警报,但我需要它来弹出一个 View 以便他们可以看到表格,但是 View 它打开的不是我的应用程序使用的两个之一。

        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];
        }

另外,所以我尝试发出一个警报,表明我可以设置委托,因此在我的控制器中,我调用上面的代码,我也调用这个:

        [[NSNotificationCenter defaultCenter]  
         postNotificationName:@"ShowAlert"
         object:nil
         userInfo:mydata];

然后拾取此通知,最终调用此函数,并且此确实有效,但我希望该警报对用户不可见,因为它位于后台。

- (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];
    }
}

在主委托中,我定义了此定义以及相应的函数:

@interface FYRViewAppDelegate : NSObject{

我的想法是,如果我可以收到弹出的警报来调用此委托,那么它就可以执行此代码:

- (void)alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
    alertview--;
    if (buttonIndex == 0) {
    } else {
        [self.window addSubview:[tableController view]];
    }
}

那么,有什么方法可以让 UILocalNotification 使用我的委托为了警报?

或者,有没有办法在应用程序处于后台时显示警报?

或者,我是否需要编写两个应用程序,以便它们与远程通知进行通信,以便当应用程序在后台时运行,然后使用远程通知启动主应用程序。我不喜欢这种方法,因为它看起来很混乱,但可能会起作用。

由于程序永远不会停止运行

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

仅在启动时调用,本地通知永远不会调用它,这也永远不会被调用:

- (void) 应用程序:(UIApplication*)应用程序 didReceiveLocalNotification:(UILocalNotification *)localNotification {

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 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 {
.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2024-11-26 00:25:52

我想您在应用程序处于活动状态时不会发布通知。难道您不能在发布本地通知时简单地设置一个标志,然后在应用程序变为活动状态时检查该标志吗:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (localNotificationPosted) {
        // Do whatever you need to do.
    }
}

这可能是正确的行为,因为除非用户关闭通知,否则应用程序将变为活动状态。即使用户忽略通知,下次打开应用程序时显示新消息也可能不是一件坏事。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文