iOS – 应用程序运行时不显示推送通知警报

我在我的应用程序中集成了推送通知。 用户将收到推送通知join一个组。 当用户点击join时 ,我必须在代码中处理一些东西。 所以我正在执行:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 

这应用程序没有运行时正常工作。
当应用程序正在运行时,我没有看到任何UIAlertView 。 如何使我的应用程序显示推送通知提醒,以便用户仍然可以决定是否join?

我在应用程序委托中使用这样的代码来模拟应用程序处于活动状态时的通知警报。 您应该实现适当的UIAlertViewDelegate协议方法来处理当用户点击任一button时发生的情况。

 - (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:@"Some title" 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 } } 

对于任何人可能有兴趣,我最终创build了一个自定义视图,看起来像顶部的系统推横幅,但添加了一个closuresbutton(小蓝X)和一个选项来点击自定义操作的消息。 它还支持在用户有时间阅读/解散旧的通知之前到达多个通知的情况(没有限制可以堆放多less…)

链接到GitHub:AGPushNote

用法基本上在内线上:

 [AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"]; 

它看起来像这样在iOS7(iOS6有一个iOS6的外观和感觉…)

例

只有这个函数会被调用,并且你必须明确地显示这种情况下的警报,如果你正在执行通知的应用程序正在运行,那么不会有任何通知。把断点放在那里,并在调用函数时处理通知调用,并显示你的定制警惕那里。

要在运行应用程序时显示警报视图,您必须使用

 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { } 

并通过访问userInfovariables

应用程序仍然会在您的应用程序委托中收到-application:didReceiveRemoteNotification消息,但是您必须自己处理消息(即,警报默认情况下不显示)。

userInfo参数包含一个关键字notificationType的对象,您可以使用该对象来标识推送消息。

这是一个支持UIAlertController的版本

 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIApplicationState state = [application applicationState]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; if (state == UIApplicationStateActive) { UIAlertController * alert= [UIAlertController alertControllerWithTitle:notification.alertTitle message:notification.alertBody preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; [alert addAction:ok]; [self.navigationController presentViewController:alert animated:YES completion:nil]; } 

}

**请注意我的应用程序在App Delegate中使用self.navigationController,只需挂钩到任何ViewController来呈现(显示)Alert **