推送通知-didFinishLaunchingWithOptions [英] Push Notification -didFinishLaunchingWithOptions

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

问题描述

当我发送推送通知和我的应用程序打开或在后台,我点击推送通知,我的应用程序重定向到 PushMessagesVc viewController (根据需要)

When I send a push notification and my app is open or in the background and I click on the push notification, my application redirects to PushMessagesVc viewController (as intended)

我使用下面的代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self.window.rootViewController presentViewController:pvc
                                                 animated:YES
                                               completion:NULL];
}

上述代码/场景没有问题,我点击一个推送通知,应用程序不会重定向我的 PushMessagesVc viewController 在这种情况&

There is no problem in the code/scenario above but if the application is closed and I click on a push notification, the application does not redirect my PushMessagesVc viewController in this case & the application stays on the main screen.

对于第二种情况,我使用以下代码:

For the 2nd scenario, I use the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    sleep(1);
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;

    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
        [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
        return YES;
    }
    return YES;
}

但在这种情况下, PushMessagesVc viewController

But in this case, the PushMessagesVc does not appear.

推荐答案

c $ c>当您收到推送通知时,您可以尝试使用 NSNotificationCenter 用于您的目的:

Since you only want to present a viewController when you get a Push Notification, you may try utilizing NSNotificationCenter for your purposes:

假设 MainMenuViewController

设置此类以侦听 NSNotification 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆