通过通知启动应用程序崩溃

时间:2015-10-14 07:29:37

标签: ios apple-push-notifications

在我的远程通知实现中,我这样做了

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

if (launchOptions) {
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    NSDictionary *dictionary=[userInfo objectForKey:@"jsonContent"];
    dm.notificationDictionary=nil;
    dm.notificationDictionary=dictionary;
    NSDictionary *aps=[userInfo objectForKey:@"aps"];
    dm.badgeNumber=[[aps valueForKey:@"badge"] intValue];
}



viewController1 = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];


UINavigationController *aNavigationController=[[UINavigationController alloc] initWithRootViewController:viewController1];

self.navigationcontroller = aNavigationController ;
self.navigationcontroller.navigationBar.hidden=YES;


[self.window setRootViewController:self.navigationcontroller];
[self.window makeKeyAndVisible];

return YES;

}

然后

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
      dm=[DataManager sharedManager];
     ws=[[WebService alloc] init];
     NSLog(@"%@",userInfo);
     dm.isFroTabNotify=NO;
     NSDictionary *dictionary=[userInfo objectForKey:@"jsonContent"];
     dm.notificationDictionary=nil;
     dm.notificationDictionary=dictionary;
     NSDictionary *aps=[userInfo objectForKey:@"aps"];
     dm.badgeNumber=[[aps valueForKey:@"badge"] intValue];
     arrayHeader=[ws GetNotificationHeaders];//--------To get unread notification count


   [[NSNotificationCenter defaultCenter] postNotificationName:@"MyPersonalNotification" object:[aps valueForKey:@"badge"]];


  if (application.applicationState==UIApplicationStateActive) {




    [AGPushNoteView showWithNotificationMessage:[dm.notificationDictionary valueForKey:@"message"]];

    if([[dm.notificationDictionary valueForKey:@"type"] intValue]==12)
    {
        dm=[DataManager sharedManager];
        dm.ifFromNotifi=YES;
        dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

        DataClass *obj=[DataClass getInstance];


        dm.isUserLoggedin=NO;
        dm.profImage=nil;
        dm=[[DataManager alloc]init];



        FrontViewController *frontViewController = [[FrontViewController alloc] init];
        RearViewController *rearViewController = [[RearViewController alloc] init];

        UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
        UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

        SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                        initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


        [self.window setRootViewController:mainRevealController];
        [self.window makeKeyAndVisible];






    }



    [AGPushNoteView setMessageAction:^(NSString *message) {
        // Do something...

        [ws setMarkAsReadUnread:[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"type"]] :[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]]];
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:dm.badgeNumber-1];
        if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==1||[[dm.notificationDictionary valueForKey:@"type"] intValue]==5) {
            dm=[DataManager sharedManager];
            dm.ifFromNotifi=YES;
            dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

            dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];

            OneBookingViewController *frontViewController = [[OneBookingViewController alloc] init];
            RearViewController *rearViewController = [[RearViewController alloc] init];

            UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
            UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

            SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                            initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];



            [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
            [self.window setRootViewController:mainRevealController];
            [self.window makeKeyAndVisible];


        }






       else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==2)
        {
            dm=[DataManager sharedManager];
            ws=[[WebService alloc] init];
            dm.ifFromNotifi=YES;



            NSString *strBidID=[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]];

            [ws GetOneBidReceived:strBidID];

            BidReceiveShowViewController *frontViewController = [[BidReceiveShowViewController alloc] init];
            RearViewController *rearViewController = [[RearViewController alloc] init];

            UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
            UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

            SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                            initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


            [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
            [self.window setRootViewController:mainRevealController];
            [self.window makeKeyAndVisible];

        }




       else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==3 || [[dm.notificationDictionary valueForKey:@"type"] intValue]==4)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;



           NSString *strBidID=[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]];

           [ws GetOneMyBid:strBidID];
           MyBidsViewViewController *frontViewController = [[MyBidsViewViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


           [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];

           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];

       }




       else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==6)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

           dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
           OneTripViewController *frontViewController = [[OneTripViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


           [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];

           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];

       }



       else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==7)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.selectedReviewID=[[dm.notificationDictionary valueForKey:@"id"] intValue];


           MyReviewsViewController *frontViewController = [[MyReviewsViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];



           [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];


       }


       else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==8)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];


           MyTravelsViewController *frontViewController = [[MyTravelsViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


           [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];

           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];

       }














    }];

}



else
{

    [ws setMarkAsReadUnread:[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"type"]] :[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]]];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:dm.badgeNumber-1];


    if ([dm.notificationDictionary count]>0) {


        if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==1||[[dm.notificationDictionary valueForKey:@"type"] intValue]==5) {
            dm=[DataManager sharedManager];
            dm.ifFromNotifi=YES;
            dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

            dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
           // dm.notificationDictionary=nil;
            OneBookingViewController *frontViewController = [[OneBookingViewController alloc] init];
            RearViewController *rearViewController = [[RearViewController alloc] init];

            UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
            UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

            SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                            initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


            dm.notificationDictionary=nil;
            [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];

            [self.window setRootViewController:mainRevealController];
            [self.window makeKeyAndVisible];


        }




       else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==2)
        {
            dm=[DataManager sharedManager];
            ws=[[WebService alloc] init];
            dm.ifFromNotifi=YES;



            NSString *strBidID=[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]];

            [ws GetOneBidReceived:strBidID];

            BidReceiveShowViewController *frontViewController = [[BidReceiveShowViewController alloc] init];
            RearViewController *rearViewController = [[RearViewController alloc] init];

            UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
            UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

            SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                            initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];

           // dm.notificationDictionary=nil;



            [self.window setRootViewController:mainRevealController];
            [self.window makeKeyAndVisible];
            [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
        }











       else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==10)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.SelectedRideID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
           dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

           RequestMatchToSellerViewController *frontViewController = [[RequestMatchToSellerViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];

           // dm.notificationDictionary=nil;


           [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];

           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];
       }


       else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==11)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.selectedRequestid=[[dm.notificationDictionary valueForKey:@"id"] intValue];
           dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

           RequestMatchToBuyerViewController *frontViewController = [[RequestMatchToBuyerViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];

           // dm.notificationDictionary=nil;


           [rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];

           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];
       }




       else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==12)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];

           DataClass *obj=[DataClass getInstance];


           dm.isUserLoggedin=NO;
           dm.profImage=nil;
           dm=[[DataManager alloc]init];
            ws=[[WebService alloc] init];

           [ws gcmDeviceRejection];
           loggedWayKeychain=[[KeychainItemWrapper alloc] initWithIdentifier:@"LoggedWay" accessGroup:nil];
           loginKeychain=[[KeychainItemWrapper alloc] initWithIdentifier:@"UserLogin" accessGroup:nil];
           [loginKeychain resetKeychainItem];
           [loggedWayKeychain resetKeychainItem];

           [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];



           FrontViewController *frontViewController = [[FrontViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];






       }

       else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==13)
       {
           dm=[DataManager sharedManager];
           dm.ifFromNotifi=YES;
           dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
           dm.strAdminMsgTitle=[dm.notificationDictionary valueForKey:@"subject"];
           dm.strAdminMsg=[dm.notificationDictionary valueForKey:@"message"];



           AdminMessagesViewController *frontViewController = [[AdminMessagesViewController alloc] init];
           RearViewController *rearViewController = [[RearViewController alloc] init];

           UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
           UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

           SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
                                                           initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];


           [self.window setRootViewController:mainRevealController];
           [self.window makeKeyAndVisible];


       }







    }

}


}

当应用程序处于后台和前台时,它可以正常工作。但是当应用程序没有运行时,当我点击通知启动应用程序时它会崩溃。这是什么原因,我该如何解决这个问题?请帮我。 感谢

1 个答案:

答案 0 :(得分:0)

正如我在方法中看到的那样 - application:didReceiveRemoteNotification:fetchCompletionHandler: 您已编码两个状态UIApplicationStates活动状态和所有其他状态(在else块中),这可能是用户杀死(强制退出)应用程序并且远程通知到达并且- application:didReceiveRemoteNotification:fetchCompletionHandler:方法为当用户点击通知警报时调用。

根据苹果文档,

  

注意:如果用户从系统显示的警报中打开您的应用程序,则   当您的应用即将进入时,系统可能会再次调用此方法   前景,以便您可以更新您的用户界面和显示   有关通知的信息。

所以,认为你应该查看代码,必须对任何导致错误的代码块进行双重调用,特别是在初始化UIViewController对象或UINavigationController对象的地方。