0

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

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

但是当应用程序处于活动状态时,它不在聊天页面中,那么我也需要本地通知,但我使用了相同的代码,通知也出现在托盘中,但横幅没有出现......

请帮帮我...

4

2 回答 2

1

以下方法放入您的应用程序委托中

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    UILocalNotification *localNotif =notification;
    NSString *strBody=[localNotif.userInfo valueForKey:@"Body"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Application name" 
                                                    message:strBody
                                                   delegate:self cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil];
    [alert show];

    //NSLog(@"Incoming notification in running app");

    // Access the payload content
    //NSLog(@"Notification payload: %@", [notification.userInfo objectForKey:@"body"]);

    application.applicationIconBadgeNumber = 0;
}
于 2012-04-24T05:54:54.720 回答
0

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

- (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;
}
于 2012-09-21T09:19:01.080 回答