2016-12-15 55 views
0

我正在嘗試處理來自我的有效內容的更多信息。這是我如何設置推送通知的iPhone和消息來通過,這是我如何創建我的有效載荷:在應用中推送有效負載信息

// Create the payload body 
$body['aps'] = array(
    'alert' => $message, 
    'sound' => 'default', 
    'link_url' => $url, 
); 

在這是我的應用程序裏面。

// WHEN a push notification comes in 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]){ 
     if let aps = userInfo["aps"] as? NSDictionary { 
      if let alert = aps["alert"] as? NSDictionary { 
       if (alert["message"] as? NSString) != nil { 
        //Do stuff 
        self.window = UIWindow(frame: UIScreen.main.bounds) 
        let storyboard = UIStoryboard(name: "Main", bundle: nil) 

        // redirect to section 
        var initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewBookings") 
        self.window?.rootViewController = initialViewController 
        self.window?.makeKeyAndVisible() 
       } 
      } else if (aps["alert"] as? NSString) != nil { 
       self.window = UIWindow(frame: UIScreen.main.bounds) 
       let storyboard = UIStoryboard(name: "Main", bundle: nil) 

       // redirect to section 
       var initialViewController2 = storyboard.instantiateViewController(withIdentifier: "ViewBookings") 
       self.window?.rootViewController = initialViewController2 
       self.window?.makeKeyAndVisible() 
      } 
     } 
    } 

但現在我需要閱讀link_url,因此我的應用可以重定向到不同的部分或推薦外部鏈接。 (它並不總是一個url) 我試過了:讓my_url = aps [「link_url」]爲? NSDictionary,然後描述爲字符串,但我什麼也得不到。

+0

是你的APS [「LINK_URL」]一本字典或字符串? –

回答

0

這取決於如何定義$ message和$ url對象。如果他們是字符串,然後下面應該工作:

if let my_url = aps["link_url"] as? String { 
    print(my_url) 
} 

從你的代碼,它看起來像你的對象如下所示:

{ "aps" : { 
    "alert" : { 
     "message" : "some message" 
    }, 
    "sound" : "default", 
    "link_url" : "www.example.com" 
    } 
}