2013-08-06 68 views
3

我正在使用推送通知处理简单的应用程序,并且我成功实现了它。当我退出应用程序时,我得到一个推送通知(工作正常),但是当我打开应用程序并尝试从我的服务器(Web应用程序)发送消息时,它不会显示任何弹出消息或通知。我错过了什么吗?这里是AppDelegate.m推送通知当应用程序正在运行时不起作用(活动)

我的代码片段
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 

// Let the device know we want to receive push notifications 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
return YES; 
} 


(void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{ 
    NSLog(@"Received notification: %@", userInfo); 
    NSString *messageAlert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; 
    NSLog(@"Received Push Badge: %@", messageAlert); 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:messageAlert]; 

} 

请帮我对此。谢谢。

+0

它不显示通知,但是您是否看到日志:“收到通知:XXX”和“收到推送徽章:YYY”(其中XXX和YYY是字符串) –

回答

5

当你的应用程序是主动模式下,你需要把这个方法有点像波纹管到你的AppDelegate类: -

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  
    UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateActive) { 
     NSString *cancelTitle = @"Close"; 
     NSString *showTitle = @"Show"; 
     NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"My App Name" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:showTitle, nil]; 
     [alertView show]; 
     [alertView release]; 


    } else { 
     //Do stuff that you would do if the application was not active 
    } 
} 

也把didFailToRegisterForRemoteNotificationsWithError委托检查故障原因

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 
NSString *str = [NSString stringWithFormat: @"Error: %@", error]; 
    NSLog(@"%@", str); 
} 
0

你对这段代码有什么期待:[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:messageAlert];

我想你想念一些基本知识..提出警报,你应该看看UIAlertViewNSNotificationCenter用于应用程序中的内部数据流。 (观察者模式)

当您的应用程序运行时,不会自动显示警报。您需要自行处理从application:didReceiveRemoteNotification:开始的推送消息。 NSNotificationCenterPush Notifications是完全不同的东西。他们不是彼此的一部分。

0

如果我正确回忆,Apple会在您的应用运行或不运行时将推送通知发送到不同的地方。您只实现了未启动的代码,但不执行执行期间的代码。

相关问题