在应用中推送有效负载信息

问题描述:

我正在尝试处理来自我的有效内容的更多信息。这是我如何设置推送通知的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”]一本字典或字符串? –

这取决于如何定义$ 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" 
    } 
}