【问题标题】:iOS receive remote notification and opens the urliOS接收远程通知并打开url
【发布时间】:2014-11-30 13:02:35
【问题描述】:

我有这个从服务器接收推送通知的 iOS 应用程序,在推送通知中它包含一个 url。现在我的应用有一个标签栏控制器和 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 在另一个视图控制器中。

【问题讨论】:

  • NsnotificationCentre 可能是最简单的

标签: ios notifications


【解决方案1】:

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

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

在需要接收通知的视图控制器中:

- (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"]]]];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多