2015-11-30 95 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