应用程序处于活动状态时的iPhone本地通知 [英] iphone local notifications when application in active

查看:44
本文介绍了应用程序处于活动状态时的iPhone本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iOS 5开发一个聊天应用程序.我在本地通知时遇到了问题.当应用程序进入后台状态时,我正在使用以下代码:

I'm developing a chat application for iOS 5. I had a problem with local notifications. When application went to background state, I am using this code:

  UILocalNotification *localNotification = [[UILocalNotification alloc] init];
  localNotification.alertAction = @"Ok";
  localNotification.alertBody = [NSString stringWithFormat:@"From: %@\n%@",message.sender,message.message];

  [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
  [localNotification release];

但是当应用程序处于活动状态,并且不在聊天页面中时,我还需要本地通知,但是我在此处也使用了相同的代码通知正在进纸匣,但没有横幅....

but when application is in active state ,and it is not in chat page then also I need the local notification but I used the same code there also the notification is coming in tray but banners are not coming....

请帮帮我...

推荐答案

以下代码可同时处理远程和本地通知

THe following code handles both remote and local notification

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [self showNotificationAlert:notification.alertBody];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSString *alertMsg = nil;
    id alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];

    if ([alert isKindOfClass:NSString.class])
        alertMsg = alert;
    else if ([alert isKindOfClass:NSDictionary.class])
        alertMsg = [alert objectForKey:@"body"];

    [self showNotificationAlert:alertMsg];
}

- (void)showNotificationAlert:(NSString *)alertMsg
{
    if (!alertMsg)
        return;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Message", nil)
                                                    message:alertMsg
                                                   delegate:self cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                          otherButtonTitles:nil];
    [alert show];

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

这篇关于应用程序处于活动状态时的iPhone本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆