简体   繁体   中英

App crash when launch it with a notification

In my remote notification implementation I have done like this

- (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;

}

Then

- (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];


       }







    }

}


}

It works fine when app is in background and foreground. But when app is not running and when I click the notification to launch the app its crashing. What is the reason for this and how can I solve this? Please help me. Thanks

As I can see in method - application:didReceiveRemoteNotification:fetchCompletionHandler: You have coded for two states UIApplicationStates active and all other states (in else block), this may be a crash when the app is killed( force quit ) by user and Remote notification arrives and - application:didReceiveRemoteNotification:fetchCompletionHandler: method is called when user tap on notification alert.

as per apple documentation,

Note: If the user opens your app from the system-displayed alert, the system may call this method again when your app is about to enter the foreground so that you can update your user interface and display information pertaining to the notification.

so, think you should look into code there must be dual call to any code block causing error, specially where you are initializing UIViewController object or UINavigationController objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM