应用程序关闭时,通知中显示UIAlertView的问题

时间:2014-09-15 00:06:22

标签: ios xcode notifications push-notification uialertview

我有以下用于推送通知的代码:

位于我的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 个答案:

答案 0 :(得分:0)

你不能因为显示一个警报视图需要通知调用你的代码的某些部分,但你的代码的入口点,即你可能首先处理有关你的通知的任何内容是

  
      
  • (BOOL)应用程序:(UIApplication )应用程序didFinishLaunchingWithOptions :( NSDictionary )launchOptions
  •   

这意味着您的应用必须醒着才能显示自定义提醒视图。

现在你必须满足ios给你的默认实现,即顶部的通知栏。

希望ts有用