首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >App在前台iOS时获取推送通知

App在前台iOS时获取推送通知
EN

Stack Overflow用户
提问于 2013-02-14 17:57:10
回答 9查看 244.3K关注 0票数 218

我正在我的应用中使用推送通知服务。当应用程序在后台时,我可以在通知屏幕上看到通知(当我们从iOS设备顶部向下滑动时显示的屏幕)。但如果应用程序在前台,则委托方法

代码语言:javascript
复制
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

正在被调用,但通知屏幕中未显示通知。

我想在通知屏幕上显示通知,而不管应用程序是在后台还是在前台。我疲于寻找解决方案。任何帮助都是非常感谢的。

EN

回答 9

Stack Overflow用户

发布于 2013-02-14 18:50:23

下面的代码将为您工作:

代码语言:javascript
复制
- (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];          
    }    
}
票数 59
EN

Stack Overflow用户

发布于 2013-02-14 18:06:11

如果应用程序在前台运行,iOS将不会显示通知横幅/警报。这是设计好的。你必须编写一些代码来处理你的应用程序在前台收到通知的情况。您应该以最合适的方式显示通知(例如,向UITabBar图标添加徽章编号、模拟通知中心横幅等)。

票数 15
EN

Stack Overflow用户

发布于 2014-03-28 22:51:18

您可以创建自己的通知来模拟横幅警报。

一种方法是创建一个自定义的uiview,它看起来像横幅,可以对触摸进行动画和响应。考虑到这一点,您可以创建具有更多功能的更好的横幅。

或者,您可以寻找一个api来为您完成此任务,并将它们作为podfile添加到您的项目中。

下面是我用过的几个例子:

https://github.com/terryworona/TWMessageBarManager

https://github.com/toursprung/TSMessages

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14872088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档