2012-04-24 63 views
0

我正在开发一个聊天应用程序iOS 5.我有一个本地通知问题。 当应用程序去后台状态,我使用这个代码:iphone本地通知当应用程序在活动

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

但是当应用程序处于活动状态,并且它不是在聊天页面,然后我也需要当地的通知,但我使用相同的代码中有也 通知在托盘来,但横幅不来....

请帮我...

回答

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; 
} 
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; 
} 
+0

通知即将作为警告,我需要的是应该是一面旗帜 – 2012-04-24 06:02:18

+0

在这种方法中,当通知是白羊座的,到时候你显示警报。 这可能对你有帮助。 – 2012-04-24 06:09:57

相关问题