iOS Webview App,如何在Webview中打开JSON Parse推送通知?

时间:2015-11-30 17:42:18

标签: ios json parse-platform webview

我有一个基本的webview应用程序正常工作并从Parse.com接收基本文本推送通知,但无法弄清楚如何让它打开URL。

我想从Parse.com发送一个JSON推送通知,并在我的webview应用程序中打开指定的URL:

编辑**我现在遇到的新问题是,如果应用在后台或前台,我无法在网页浏览中打开网址。

目前,如果应用程序在后台并点击通知,它只会将应用程序带到前台但不会重新加载新URL。如果应用程序位于前台,则会将通知作为警报接收,当您点击" OK"时,不会执行任何操作。

{
"alert": "Push title goes here", 
"url": "http://www.google.com" 
}

在我的 WebBrowserAppDelegate.m 中,我有这个:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [Parse setApplicationId:@"XXX"
                      clientKey:@"XXX"];

        // Register for Push Notitications
        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                                 categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];

        // Override point for customization after application launch.

        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    NSString *pushURL = [notificationPayload objectForKey:@"url"];

    if (notification) {
        NSDictionary *aDict=[NSDictionary dictionaryWithObject: pushURL forKey:@"urlToLoad"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LoadRequestFromAppDel" object:Nil userInfo:aDict];
    }

    return YES;
    }
     - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        [PFPush handlePush:userInfo];
        }

在我的 WebBrowserViewController.m 中,我有这个:

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSAssert(self.back, @"Unconnected IBOutlet 'back'");
        NSAssert(self.forward, @"Unconnected IBOutlet 'forward'");
        NSAssert(self.refresh, @"Unconnected IBOutlet 'refresh'");
        NSAssert(self.stop, @"Unconnected IBOutlet 'stop'");
        NSAssert(self.webView, @"Unconnected IBOutlet 'webView'");

        self.webView.delegate = self;
        self.webView.scalesPageToFit = YES;
        NSURL* url = [NSURL URLWithString:@"http://www.mywebsite.com"];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
        [self updateButtons];
    }

- (void)LoadRequestFromAppDel: (NSNotification*)aNotif
{
    NSString *aStrUrl=[[aNotif userInfo] objectForKey:@"urlToLoad"];
    NSURL* pushurl = [NSURL URLWithString:aStrUrl];
    NSURLRequest* requestObj = [NSURLRequest requestWithURL:pushurl];
    [self.webView loadRequest:requestObj];
    [self updateButtons];
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这是使用parse.com截取远程推送的方法:

this

在上面的示例中,在您的情况下,在“if(notification)”触发器的第一部分中,您将初始化自定义Web视图控制器然后显示或设置或推送它,但是您需要,当APP被用户完全关闭时,现在使用Parse Cloud代码触发器验证这是正常工作。因此,这意味着此通知甚至会在完全关闭的应用上触发。当应用程序处于后台时,我不知道这是否会起作用,但是当它完全关闭时,它将起作用。