我正在我的应用中使用推送通知服务。当应用程序在后台时,我可以在通知屏幕上看到通知(当我们从iOS设备顶部向下滑动时显示的屏幕)。但如果应用程序在前台,则委托方法
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
正在被调用,但通知屏幕中未显示通知。
我想在通知屏幕上显示通知,而不管应用程序是在后台还是在前台。我疲于寻找解决方案。任何帮助都是非常感谢的。
发布于 2013-02-14 18:50:23
下面的代码将为您工作:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive) {
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
发布于 2013-02-14 18:06:11
如果应用程序在前台运行,iOS将不会显示通知横幅/警报。这是设计好的。你必须编写一些代码来处理你的应用程序在前台收到通知的情况。您应该以最合适的方式显示通知(例如,向UITabBar
图标添加徽章编号、模拟通知中心横幅等)。
发布于 2014-03-28 22:51:18
您可以创建自己的通知来模拟横幅警报。
一种方法是创建一个自定义的uiview,它看起来像横幅,可以对触摸进行动画和响应。考虑到这一点,您可以创建具有更多功能的更好的横幅。
或者,您可以寻找一个api来为您完成此任务,并将它们作为podfile添加到您的项目中。
下面是我用过的几个例子:
https://stackoverflow.com/questions/14872088
复制相似问题