ios - 在 Webview 中打开指定的 URL(iOS 推送)

标签 ios user-interface webview push

在我的 iOS 应用程序中,我有一个 UIWebView 所以我如何在 UIWebView 中打开指定的 URL,使用 推送通知?

如果有人用notification 打开应用程序,我想在 UIWebView

我可以将URL(在后台)与推送通知绑定(bind)吗?

谢谢。

最佳答案

根据苹果...

If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action. ... If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the push notification payload data and respond appropriately.

因此,存在三种可能的情况:

1) 应用程序在前台:您将拥有完全的控制权,只需实现 didReceiveNotification 并执行任何您想做的事情。

2) 应用正在运行,但在后台:直到用户使用收到的通知实际打开您的应用时,才会触发操作。

3) 应用未运行:在这种情况下,您应该实现 didFinishLaunchingWithOptions 以获取附加信息并执行任务。

所以 didFinishLaunchingWithOptions 的代码应该是这样的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        // Get the URL or any other data
    }
}

这是 didReceiveNotification 的近似值

/**
 * Remote Notification Received while application was open.
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    #if !TARGET_IPHONE_SIMULATOR

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive)
    {
        NSString *message = nil;
        id aps = [userInfo objectForKey:@"aps"];
        if ([aps isKindOfClass:[NSDictionary class]]) {
            message = [aps objectForKey:@"alert"];
        }

        if (message) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notificación"
                                                                message:message
                                                               delegate:self
                                                cancelButtonTitle:@"Aceptar"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
        }
    }

    // Aditional data
    NSString *url = [userInfo objectForKey:@"url"];
    NSLog(@"Received Push URL: %@", url);

    if(url!=nil)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }

    NSLog(@"remote notification: %@",[userInfo description]);
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    NSString *alert = [apsInfo objectForKey:@"alert"];
    NSLog(@"Received Push Alert: %@", alert);

    NSString *sound = [apsInfo objectForKey:@"sound"];
    NSLog(@"Received Push Sound: %@", sound);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);
    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];

    #endif
}

关于ios - 在 Webview 中打开指定的 URL(iOS 推送),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23387157/

相关文章:

java - 在运行时更改 JComboBox 的内容

java - 为什么我可以在一个应用程序中看到我的网站,但在另一应用程序中却看不到?

java - 代码else eclipse---->删除 token

ios - 在 iPhone 锁定和休眠时测距和监控信标

iOS联系人如何通过电话号码获取联系人

java - token "-"出现语法错误,AssignmentOperator 无效

java - 在发生其他事情之前必须完成的对话

ios - [FBSDKLoginManager logInWithReadPermissions :fromViewController:handler:]: unrecognized selector sent to instance

ios - 检测到约束模糊地表明高度为零的情况

android - 错误-从 Android Webview 中的 URL 加载 PDF