gpt4 book ai didi

ios - 在 AppDelegate 中使用推送通知重定向 View

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:05 25 4
gpt4 key购买 nike

我的目标是编写一段代码,当用户收到推送通知时,我希望将该用户重定向到另一个 View 。如果用户带有推送通知并且他已经第一次查看 Controller (欢迎主屏幕等(但未登录))

var rootViewController = self.window!.rootViewController as! ViewController
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)

这几行代码是有效的,但是,如果用户已经在另一个 View Controller 中(登录/登录/用户页面等),这段代码将不起作用并重定向。我尝试了一切,但仍然无法提出解决方案。我的最终目标是:

if let rootViewController = self.window!.rootViewController as? ViewController
{
var rootView: UserViewController = UserViewController()

if let window = self.window{
window.rootViewController = rootView
}

rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
println(self.window?.rootViewController)
}

谁能给我一个主意?

最佳答案

答案代码是Objective-C,我想说说逻辑。为此,在创建推送通知时,您必须设置 userInfo 值以通过推送通知传递数据。

使用这个委托(delegate)方法:application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

您可以通过处理userInfo 来决定是否要显示哪个 View

NSDictionary *segueDictionary = [userInfo valueForKey:@"aps"];

NSString *segueName=[[NSString alloc]initWithFormat:@"%@",[segueDictionary valueForKey:@"segueName"]];

if([segueName isEqualToString:@"hospitalSegue"])
{
// implement your code to redirect
}

关于ios - 在 AppDelegate 中使用推送通知重定向 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30617215/

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