ios - iOS,先强制退出应用程序,点击接收通知横幅后,应用程序启动失败

标签 ios notifications crash apple-push-notifications swipe

我使用APNs将通知发送到我的应用程序。但是,当我执行以下步骤时,我的应用程序无法正常运行:

脚步

  • 滑动应用以强制退出(应用未运行,不在后台模式下..)
  • 从APN发送通知
  • 在我的iPhone上收到了通知,然后我轻按了通知横幅
  • 应用程序似乎尝试启动(显示启动图像),但是启动失败(崩溃?)

  • 我的应用程序可以接收通知的前台和后台。
    点按背景中的通知横幅,然后它可以将应用程序置于前台,然后转到我编写的 View ,一切正常。

    除了强制退出外,APP

    这是我在AppDelegate.m中的代码
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                        containerWithCenterViewController:[self navigationController]
                                                        leftMenuViewController:leftMenuViewController
                                                        rightMenuViewController:rightMenuViewController];
    
        self.window.rootViewController = container;
        [self.window makeKeyAndVisible];
    
        UIMutableUserNotificationAction *acceptAction =
        [[UIMutableUserNotificationAction alloc] init];
    
        // Define an ID string to be passed back to your app when you handle the action
        acceptAction.identifier = @"MARK_AS_READ_IDENTIFIER";
    
        // Localized string displayed in the action button
        acceptAction.title = NSLocalizedString(@"Mark as Read", nil);
    
        // If you need to show UI, choose foreground
        acceptAction.activationMode = UIUserNotificationActivationModeBackground;
    
        // Destructive actions display in red
        acceptAction.destructive = NO;
    
        // Set whether the action requires the user to authenticate
        acceptAction.authenticationRequired = NO;
    
        // First create the category
        UIMutableUserNotificationCategory *inviteCategory =
        [[UIMutableUserNotificationCategory alloc] init];
    
        // Identifier to include in your push payload and local notification
        inviteCategory.identifier = @"actionCategory";
    
        // Add the actions to the category and set the action context
        [inviteCategory setActions:@[acceptAction]
                        forContext:UIUserNotificationActionContextDefault];
    
        // Set the actions to present in a minimal context
        [inviteCategory setActions:@[acceptAction]
                        forContext:UIUserNotificationActionContextMinimal];
        NSSet *categories = [NSSet setWithObject:inviteCategory];
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert) categories:categories]];
    
        // for calling didReceiveRemoteNotification when app first launch
        if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
            [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
        }
        return YES;
    }
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
        //register to receive notifications
        [application registerForRemoteNotifications];
    }
    
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
            NSLog(@"Device token: %@",deviceToken);
    }
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSLog(@"Fail to get device token: %@", error);
    }
    
    // tap the backgraund banner button
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)newUserInfo completionHandler:(void (^)())completionHandler {
        if ([identifier isEqualToString:@"MARK_AS_READ_IDENTIFIER"]) {
            // when tapping the background banner's button will mark the notification status to read
            [Functions updateComingNotificationToRead];
        }
    
        if (completionHandler) {
            completionHandler();
        }
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)newUserInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
       //NSLog(@"Notification received: %@ %@", newUserInfo,[newUserInfo objectForKey:@"aps"] );
        userInfo = newUserInfo;
    
        NSString *alertMessage = [[newUserInfo objectForKey:@"aps"]  objectForKey:@"alert"];
        UIApplicationState state = [application applicationState];
        UIAlertView *alertView = nil;
    
        // for background banner use
        switch (state) {
            case UIApplicationStateActive: // when app is alive, show alert to notify user
                alertView =  [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SmartHome",nil) message:NSLocalizedString(alertMessage,nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:NSLocalizedString(@"Open",nil),NSLocalizedString(@"Mark as Read",nil), nil];
                [alertView show];
                break;
            case UIApplicationStateBackground: // app is in background mode
                // user tap the banner or tap the mark as read button, code will go here
                [Functions addNotificationDataInDatabase:[newUserInfo objectForKey:@"uniqueID"] type:[newUserInfo objectForKey:@"deviceType"] event:[newUserInfo objectForKey:@"event"] time:[newUserInfo objectForKey:@"time"] read:@"0" description:alertMessage];
                break;
            case UIApplicationStateInactive: // tapping the banner
                //NSLog(@"UIApplicationStateInactive");
                // go to notification view
            // because will go to the notification view detail, set status to read
                [self gotoNotificationView:userInfo]; //uniqueID
                break;
            default:
                break;
        }
    
        // Set icon badge number to zero
        application.applicationIconBadgeNumber = 0;
    
        // Handle the received message
        // Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
        handler(UIBackgroundFetchResultNoData);
    
        // send post notification for updating the badge, will get the notification in foreground
        [[NSNotificationCenter defaultCenter] postNotificationName:@"APNsNotification" object:self userInfo:nil];
    
    }
    

    有人以前有这个问题吗?我错过了什么?

    请帮我!!

    最佳答案

    您可以发出警报并检查launchOptions

    if (launchOptions) {
        if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
            [self application:self didReceiveRemoteNotification:[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] fetchCompletionHandler:^(UIBackgroundFetchResult result) {
    
            }];
        }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)newUserInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
    
    if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
    NSString *str = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    }
    }
    

    关于ios - iOS,先强制退出应用程序,点击接收通知横幅后,应用程序启动失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34089349/

    相关文章:

    ios - 用新行替换 NSString 中的 <br>

    java - NotificationCompat - 如何添加没有图标的操作?

    ios 火灾前本地通知检查

    iphone - 呈现新 Controller 时取消所有模态转换

    iphone - 应用程序在 iPhone 上崩溃但在模拟器上没有

    ios - 无法在 plist 文件 xcode 5 中添加行

    ios - 表中的加号和减号

    ios - Swift - 如何在文本字段外点击后关闭数字键盘

    c++ - 使用 http post 在 FCM 中推送通知

    android - Flutter 应用程序因 std::bad_alloc 中止消息而崩溃