UIApplication didReceiveRemoteNotification,显示应用内横幅
Posted
技术标签:
【中文标题】UIApplication didReceiveRemoteNotification,显示应用内横幅【英文标题】:UIApplication didReceiveRemoteNotification, show an in-app banner 【发布时间】:2014-07-16 00:57:07 【问题描述】:我想在我的应用玩游戏期间显示应用内横幅通知,而不是我当前的UIAlertView
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
if (application.applicationState == UIApplicationStateActive )
// Show Alert ->
// UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Did receive a Remote Notification", nil)
// message:[apsInfo objectForKey:@"alert"]
// delegate:self
// cancelButtonTitle:NSLocalizedString(@"OK", nil)
// otherButtonTitles:nil];
// [alertView show];
// [alertView release];
// Show Banner Notification
这是我想要实现的示例:
如何在我的应用玩游戏期间实现应用内横幅通知?
【问题讨论】:
你试过什么?我建议使用带有多行UILabel
的 UIView
How to show remote push notification as a banner style in active state of app? 的可能重复项
谢谢。部队231。这对我有好处。
与您发布的不太一样,但有一个不错的 github 项目:github.com/toursprung/TSMessages 也可用作 cocoapods。
【参考方案1】:
#define BANNER_HEIGHT 66.0
if (!showingNotification)
showingNotification = YES;
// retrieve message from your actual notification here
NSString *message = @"Showing notification";
UIToolbar *newmessageBannerView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -BANNER_HEIGHT, self.view.frame.size.width, BANNER_HEIGHT)];
newmessageBannerView.translucent = YES;
newmessageBannerView.barStyle = UIBarStyleBlack;
newmessageBannerView.backgroundColor = [UIColor blackColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 33.0, 16.0, 16.0)];
imageView.image = [UIImage imageNamed:@"icon_72"];
[newmessageBannerView addSubview:imageView];
[self.view addSubview:newmessageBannerView];
UILabel *bannerLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0, 30.0, 320.0, 22.0)];
bannerLabel.textAlignment = NSTextAlignmentLeft;
bannerLabel.textColor = [UIColor whiteColor];
bannerLabel.font = [UIFont systemFontOfSize:17.0];
bannerLabel.text = message;
bannerLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
bannerLabel.adjustsFontSizeToFitWidth = YES;
[newmessageBannerView addSubview:bannerLabel];
[UIView animateWithDuration:0.25 delay:0.1 options:UIViewAnimationOptionCurveLinear
animations:^
newmessageBannerView.frame = CGRectMake(0, 0, self.view.frame.size.width, BANNER_HEIGHT);
completion:^(BOOL finished)
[UIView animateWithDuration:0.25 delay:2.0 options:UIViewAnimationOptionCurveLinear
animations:^
newmessageBannerView.frame = CGRectMake(0, -BANNER_HEIGHT, self.view.frame.size.width, BANNER_HEIGHT);
completion:^(BOOL finished)
[newmessageBannerView removeFromSuperview];
showingNotification = NO;
];
];
我尝试测试 ios 6.0、7.04、7.1。
【讨论】:
以上是关于UIApplication didReceiveRemoteNotification,显示应用内横幅的主要内容,如果未能解决你的问题,请参考以下文章
UIApplication.sharedApplication.delegate.window 和 UIApplication.sharedApplication.keyWindow 有啥区别?