gpt4 book ai didi

iOS 接收远程通知并打开 url

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:52 25 4
gpt4 key购买 nike

我有这个从服务器接收推送通知的 iOS 应用程序,在推送通知中它包含一个 url。现在我的应用程序有一个标签栏 Controller 和 4 个标签,每个标签包含一个 WebView ,如何在收到推送通知后立即打开 url 并在其中一个 WebView 中加载页面?

我能够像这样从通知消息中获取 url:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"userInfo: %@",[userInfo description]);
NSLog(@"alert: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
NSLog(@"alert: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"url"]);
}

但是我卡在这里了,因为我不知道如何让 webview 知道现在是时候加载这个 url 了。请注意,推送通知处理发生在 appDelegate.m 中,但我的 WebView 在另一个 View Controller 中。

最佳答案

感谢 Paulw11,我已经设法使用 NSNotification 解决了这个问题:在 appDelegate.m 中:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedPushNotification" object:userInfo];
}

在需要接收通知的 View Controller 中:

- (void)viewDidLoad
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performTask:) name:@"ReceivedPushNotification" object:nil];

}
- (IBAction)performTask:(NSNotification *) notification
{
NSLog(@"notification received");
NSLog(@"%@", notification.object);
NSLog(@"alert: %@", [[notification.object objectForKey:@"aps"] objectForKey:@"url"]);
[self.webview_main loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[notification.object objectForKey:@"aps"] objectForKey:@"url"]]]];
}

关于iOS 接收远程通知并打开 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27209878/

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