gpt4 book ai didi

ios - 推送通知 -didFinishLaunchingWithOptions

转载 作者:技术小花猫 更新时间:2023-10-29 10:14:38 26 4
gpt4 key购买 nike

当我发送推送通知并且我的应用程序处于打开状态或在后台并单击推送通知时,我的应用程序将重定向到 PushMessagesVc viewController (如预期的那样)

我使用如下代码:

-(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在这种情况下,应用程序保留在主屏幕上。

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

- (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,你可以尝试使用 NSNotificationCenter 来实现你的目的:

第 1 部分:设置一个类(在您的情况下为 rootViewController)以监听/响应 NSNotification

假设,MainMenuViewController 是您的navigationControllerrootViewController
设置此类以监听 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];
}

第 2 部分:发布通知(可以从代码中的任何位置)

在您的情况下,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];
}

PS:我还没有为我的项目这样做(),但它有效并且是我能想到的最好的方式(目前>)

关于ios - 推送通知 -didFinishLaunchingWithOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20771312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com