本文介绍了推送通知-didFinishLaunchingWithOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发送推送通知和我的应用程序打开或在后台,我点击推送通知,我的应用程序重定向到 c> rootViewController / code>:

Suppose, MainMenuViewController is the rootViewController of your navigationController.
Set up this class to listen to a NSNotification:

- (void)viewDidLoad {
    //...
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(presentMyViewOnPushNotification)
                                                 name:@"HAS_PUSH_NOTIFICATION"
                                               object:nil];
}

-(void)presentMyViewOnPushNotification {
    //The following code is no longer in AppDelegate
    //it should be in the rootViewController class (or wherever you want)

    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self presentViewController:pvc animated:YES completion:nil];
    //either presentViewController (above) or pushViewController (below)
    //[self.navigationController pushViewController:pvc animated:YES];
}






()



在您的情况下, AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //firstly, don't sleep the thread, it's pointless
    //sleep(1); //remove this line

    if (launchOptions) { //launchOptions is not nil
        NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        if (apsInfo) { //apsInfo is not nil
            [self performSelector:@selector(postNotificationToPresentPushMessagesVC) 
                       withObject:nil
                       afterDelay:1];
        }
    }
    return YES;
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //this method can be done using the notification as well
    [self postNotificationToPresentPushMessagesVC];
}

-(void)postNotificationToPresentPushMessagesVC {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil];
}






t为我的项目( )做这个,但它的工作原理,是我能想到做这种东西的最好的方式(


PS: I haven't done this for my projects (yet) but it works and is the best way i could think of doing this kinda stuff (for the moment)

这篇关于推送通知-didFinishLaunchingWithOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:14