应用关闭时从通知中显示 UIAlertView 的问题
Posted
技术标签:
【中文标题】应用关闭时从通知中显示 UIAlertView 的问题【英文标题】:Problems displaying UIAlertView from notification when app is closed 【发布时间】:2014-09-15 00:06:21 【问题描述】:我有以下用于推送通知的代码:
位于我的 appdelegate.m 中的是以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after application launch.
//Register to receive notifcations
//-- Set Notification
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
[Pushbots getInstance];
NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo)
// Notification Message
NSString* notificationMsg = [userInfo valueForKey:@"message"];
// Custom Field
NSString* title = [userInfo valueForKey:@"title"];
NSLog(@"Notification Msg is %@ and Custom field title = %@", notificationMsg , title);
return YES;
-(void)onReceivePushNotification:(NSDictionary *) pushDict andPayload:(NSDictionary *)payload
[payload valueForKey:@"title"];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"New Alert !" message:[pushDict valueForKey:@"alert"] delegate:self cancelButtonTitle:@"Thanks !" otherButtonTitles: @"Open",nil];
[message show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Open"])
[[Pushbots getInstance] OpenedNotification];
// set Badge to 0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
// reset badge on the server
[[Pushbots getInstance] resetBadgeCount];
当应用程序未关闭时,此代码可以正常工作,它会向我显示通知警报视图。
但是,当应用程序完全关闭且不在后台运行时,它无法正常工作。
我不知道该怎么办!
提前谢谢?
【问题讨论】:
当应用未运行或不在后台时代码如何执行? 【参考方案1】:你不能,因为显示警报视图需要通知调用你的代码的某些部分,但是你的代码的入口点,即你可以首先处理与你的通知有关的任何事情的地方
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary)launchOptions
这意味着您的应用必须处于唤醒状态才能显示自定义警报视图。
现在你必须适应 ios 给你的默认实现,即顶部的通知栏。
希望有用
【讨论】:
以上是关于应用关闭时从通知中显示 UIAlertView 的问题的主要内容,如果未能解决你的问题,请参考以下文章