ios - 通过推送通知 Objective C iOS 从 Kill 状态打开时应用程序崩溃

标签 ios objective-c xcode apple-push-notifications

我的应用程序在后台运行时运行良好,并通过单击推送通知打开它。但是,如果我终止应用程序并通过推送通知打开它,那么应用程序就会崩溃。我还集成了 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;
}

enter image description here

最佳答案

问题是您从 didFinishLaunchingWithOptions 方法的 launchOptions 获得打开应用程序的通知,并传递给打开应用程序时处理它的相同函数并执行push/present 这会使应用程序崩溃,因为尚未设置 Windows 的 Root View Controller ,因此请延迟直到应用程序处于事件状态

关于ios - 通过推送通知 Objective C iOS 从 Kill 状态打开时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48788705/

相关文章:

ios - 将 TextField 输入限制为大写字母、数字和 6 个字符限制?

ios - 2 个完全相同的 iOS 应用程序,除了数据库不同

ios - Xcode 构建失败, fatal error : module 'firebase_auth' not found @import firebase_auth;

ios - 在 [superview drawRect :]? 内调用 [subview setNeedsDisplay] 是否安全

ios - 使用 Swift 将 NSString 转换为 CLLocationCoordinate2DMake

ios - WatchKit 通知窗框颜色

ios - 如何使用 RestKit Testing 测试此对象映射

ios - 在 xcode5 中找不到适用于 ios 5 的 Storyboard

ios - 使用 UIAppearance 为 UITableViewCell 文本设置样式

Xcode Interface Builder 无法启动