2015-11-30 64 views
0

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

我想从Parse.com发送JSON推送通知是这样的事情,并把它在我的WebView应用程序打开指定的URL:

编辑**我现在有新的问题是我”如果应用程序位于后台或前台,则无法在Web视图中打开该网址。

目前,如果应用程序在后台,并且点击通知,它会将应用程序置于前台,但不会重新加载新的URL。如果应用程序处于前台,则会以通知的形式接收通知,而点击“确定”时不会发生任何操作。

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

感谢您的帮助!

+0

你需要拦截来自解析的响应。我会告诉你一秒钟。 – Loxx

回答

1

这是您使用parse.com如何截取远程推送:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; 
    NSString *productId = [notificationPayload objectForKey:@"p"]; 

    if (notification) { 
    //this will only trigger when the app has been opend from a remote PUSH notification!! 

     [self application:application didReceiveRemoteNotification:(NSDictionary*)notification]; 
     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     [_window makeKeyAndVisible]; 

     _customRootViewController = [EZPUSHEXAMPLENavigationController new]; 
     [_customRootViewController setNavigationBarHidden:true]; 
     [self.window setRootViewController:_customRootViewController]; 

     if ([PFUser currentUser] == nil) { 
      [self registerForNotifications:application launchOptions:launchOptions]; 
      EZPUSHEXAMPLEPreSplashViewController * splashScreen = [[EZPUSHEXAMPLEPreSplashViewController alloc] init]; 
      [_customRootViewController setViewControllers:@[splashScreen] animated:TRUE]; 
     } else { 
      EZPUSHEXAMPLEPreSplashViewController * splashScreen = [[EZPUSHEXAMPLEPreSplashViewController alloc] init]; 
      EZPUSHEXAMPLECustomTabBarController * mvc = [[EZPUSHEXAMPLECustomTabBarController alloc] init]; 
      [mvc.navigationController.navigationBar setTranslucent:TRUE]; 
      [mvc.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]]; 
      [(EZPUSHEXAMPLEFeedViewController*)[[[[mvc viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0] setProductId:productId]; 
      [_customRootViewController setViewControllers:@[splashScreen, mvc] animated:TRUE]; 
    } 
     return YES; 
    } else { 
     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     [_window makeKeyAndVisible]; 
     _customRootViewController = [EZPUSHEXAMPLENavigationController new]; 
     [_customRootViewController setNavigationBarHidden:true]; 
     [self.window setRootViewController:_customRootViewController]; 
     if ([PFUser currentUser] == nil) { 
      [self registerForNotifications:application launchOptions:launchOptions]; 
      EZPUSHEXAMPLEPreSplashViewController * splashScreen = [[EZPUSHEXAMPLEPreSplashViewController alloc] init]; 
      [_customRootViewController setViewControllers:@[splashScreen] animated:TRUE]; 
     } else { 
      EZPUSHEXAMPLEPreSplashViewController * splashScreen = [[EZPUSHEXAMPLEPreSplashViewController alloc] init]; 
      EZPUSHEXAMPLECustomTabBarController * mvc = [[EZPUSHEXAMPLECustomTabBarController alloc] init]; 
      [mvc.navigationController.navigationBar setTranslucent:TRUE]; 
      [mvc.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]]; 
      [_customRootViewController setViewControllers:@[splashScreen, mvc] animated:TRUE]; 
     } 
     return YES; 
    } 
} 

在上面的例子中,在你的情况下,在的“如果(通知)”触发,你会初始化的第一部分您的自定义Web视图控制器,然后呈现它,或者设置它或推送它,但是您想要的是,当APP完全由用户关闭时,这被验证为正在使用Parse Cloud代码触发器用于推送通知事件。所以,这意味着这个通知甚至会触发一个完全关闭的应用程序。我不知道这个应用程序在后台运行时是否可以工作,但是它会在完全关闭时运行。

+0

感谢您的回复!现在就去测试一下,对不起,如果这是一个愚蠢的问题,但我会放在我的WebBrowserAppDelegate.m下面我设置我的解析ApplicationId和ClientKey? – AJ47

+0

基本上,是的,只是if/then部分和if/then部分之前的声明,然后确保从推词典中“回顾正确的键”,在我的情况下,“p”是一个自定义字段从云代码推送的字典。 – Loxx

+0

然后,还有一些非常强烈的类型转换我正在使用的方法调用。你不应该在你的实例中使用类似这样的方法,很可能,仅仅是因为它现在会让你更加困惑,但是你的想法是在userinfodictionary中发送一个自定义对象消息字符串值JSON推送你从Parse发送。然后,您拦截此消息并查找您的自定义字段值,然后根据此自定义字符串值进行工作或更改您希望的操作。 – Loxx