簡體   English   中英

通過推送通知Objective C iOS從“終止”狀態打開時,應用崩潰

[英]App crash when open from Kill state through Push notification Objective C iOS

當我的應用程序在后台運行時,我的應用程序運行正常,並通過單擊“推送通知”將其打開。 但是,如果我終止應用程序並通過推送通知將其打開,則應用程序將崩潰。 我還集成了Crashlytics。 這是隨附的照片和代碼。 應用程序僅在一種通知中崩潰,這不是數據解析問題。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!error) {
                                  NSLog(@"request authorization succeeded!");
                                  //[self showAlert];
                              }
                          }];




// Branch Intigration
    Branch *branch = [Branch getInstance];

//    if is debug
//    [[Branch getInstance] setDebug];


    [branch initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
        if (!error && params) {
            DecisionController * mainVC = [[DecisionController alloc] init];
            [mainVC viewDidLoad];
            // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
            // params will be empty if no data found
            // ... insert custom logic here ...
            NSString * non_branch_link  = [params objectForKey:@"+clicked_branch_link"];
            if ([[params objectForKey:@"+clicked_branch_link"] boolValue]) {
                NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
                [nc postNotificationName:@"DeepLinkNotification" object:self userInfo:params];
            }
            NSLog(@"params: %@", params.description);
        }
    }];

    // MAGICAL RECORD Intigration
    [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"EmergencMeDatabase.sqlite"];
//    [Fabric with:@[[branch class]]];
    NSLog(@"magical record path to SQLite base %@", [NSPersistentStore MR_defaultLocalStoreUrl]);
    [Fabric with:@[[Crashlytics class]]];

    [SharedRenderer setValue:@"NO" key:isForRedar];
    [[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"lastnotificationminute"];
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    [MPNotificationView registerNibNameOrClass:@"CustomNotificationView"
                        forNotificationsOfType:@"Custom"];
    [self registerForRemoteNotifications];
    [SharedRenderer sharedInstance].retryCount = 0;
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"generatenotification"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstSubscribe"];
    mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                               bundle: nil];
    if (launchOptions[UIApplicationLaunchOptionsLocationKey]) {

        [self startLocationManager:@""];
    }
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [SharedRenderer setValue:@"0" key:@"alldatafetched"];
        [SharedRenderer setValue:@"" key:@"currentchannel"];
        NSMutableArray* alerts = [[NSMutableArray alloc] init];
        NSData* alertsData=[NSKeyedArchiver archivedDataWithRootObject:alerts];
        [[NSUserDefaults standardUserDefaults] setObject:alertsData forKey:@"nonreasonalerts"];
        NSMutableArray* receivedAlerts = [[NSMutableArray alloc] init];
        NSData* receivedAlertsData=[NSKeyedArchiver archivedDataWithRootObject:receivedAlerts];
        [[NSUserDefaults standardUserDefaults] setObject:receivedAlertsData forKey:@"receivedalerts"];
        NSMutableArray* myAlerts = [[NSMutableArray alloc] init];
        NSData* myAlertsData=[NSKeyedArchiver archivedDataWithRootObject:myAlerts];
        [[NSUserDefaults standardUserDefaults] setObject:myAlertsData forKey:@"myalerts"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"OB"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    if (![[SharedRenderer getValueFor:@"isLogin"] boolValue])
    {
        navigationController = [mainStoryboard instantiateViewControllerWithIdentifier:@"loginVC"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showhomealert"];
    }
    else
    {
        if ([[SharedRenderer getValueFor:@"family_id"] isEqualToString:@""] || [[SharedRenderer getValueFor:@"alldatafetched"] isEqualToString:@"0"])
        {
            navigationController = [mainStoryboard instantiateViewControllerWithIdentifier:@"Smarticestep1"];
        }
        else
        {
            navigationController = [mainStoryboard instantiateViewControllerWithIdentifier:@"homenav"];
            [self activatedeacAlertTimer];
            [self activatePendingAlertTimer];
            [self checkFor_AlertDeactivation];
            [self fetchInvitationCount];
            // Mark by Mian:
            //            [self updatePSAlertTable];
        }
    }
    [self.window setRootViewController: navigationController];
    [self.window makeKeyAndVisible];
    LeftMenuViewController *leftMenu = (LeftMenuViewController*)[mainStoryboard
                                                                 instantiateViewControllerWithIdentifier: @"LeftMenuViewController"];
    RightMenuViewController *rightMenu = (RightMenuViewController*)[mainStoryboard
                                                                    instantiateViewControllerWithIdentifier: @"RightMenuViewController"];
    [SlideNavigationController sharedInstance].rightMenu = rightMenu;
    [SlideNavigationController sharedInstance].leftMenu = leftMenu;
    [SlideNavigationController sharedInstance].menuRevealAnimationDuration = .4;

    // Creating a custom bar button for right menu
    UIButton *button  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [button setImage:[UIImage imageNamed:@"gear"] forState:UIControlStateNormal];
    [button addTarget:[SlideNavigationController sharedInstance] action:@selector(toggleRightMenu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [SlideNavigationController sharedInstance].rightBarButtonItem = rightBarButtonItem;
    [[NSNotificationCenter defaultCenter] addObserverForName:SlideNavigationControllerDidClose object:nil queue:nil usingBlock:^(NSNotification *note)
     {
         //         NSString *menu = note.userInfo[@"menu"];
     }];

    [[NSNotificationCenter defaultCenter] addObserverForName:SlideNavigationControllerDidOpen object:nil queue:nil usingBlock:^(NSNotification *note)
     {
         //         NSString *menu = note.userInfo[@"menu"];
     }];

    [[NSNotificationCenter defaultCenter] addObserverForName:SlideNavigationControllerDidReveal object:nil queue:nil usingBlock:^(NSNotification *note)
     {
         //         NSString *menu = note.userInfo[@"menu"];
     }];
    [self ConnectPusher];
    if ([[SharedRenderer getValueFor:@"isLogin"] boolValue]){
        [self checkFor_AlertDeactivation];
        [self removePastAlert];
        [self deactivateAlert];
        [self goOnline_Offline:@"1"];
    }
    [self showAlert:launchOptions];
    return YES;
}

在此處輸入圖片說明

問題是,您會從didFinishLaunchingWithOptions方法的launchOptions中獲取打開應用程序的通知,並在打開應用程序時傳遞給處理該函數的相同功能,並執行push / present操作,因為尚未設置Windows的根視圖控制器,導致應用程序崩潰,因此請延遲到應用激活之前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM