2015-12-29 74 views
3

我试图通过解析网站从我的网站接收我的自定义通知。接收自定义通知iOS swift 2.0

代码didReceiveRemoteNotification:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
     PFPush.handlePush(userInfo) 
     if application.applicationState == UIApplicationState.Inactive { 
     PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)} 

这里是从parse网站收到我的JSON:

[aps: { 
    alert = "test adirax"; 
    sound = default; }] 

它很适合我,并且通知显示出来。但是,当我试图从我的网站推送数据时,通知无法显示/弹出。

这里是我的JSON的样子:

{"aps": {"alerts" : "test", 
     "links": "", 
     "sounds": "default"}} 

我试图打印(用户信息),结果是我得到的所有数据像上面,但没有通知符。

我想我错过了一些代码来转换数据?

信息

有关的特定信息,我试图通过订阅“信道”接收来自parse.com通知。所以它不是本地通知或其他。

ADD代码添加(根据我的JSON类型)

if let launchOptions = launchOptions { 
      let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject] 
      let aps = userInfo!["aps"] as? [NSObject: AnyObject] 
      let alert1 = aps!["alerts"] as? String 
      let link1 = aps!["links"] as? String 
     } 

而且这样的:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 

     PFPush.handlePush(userInfo) 
     if application.applicationState == UIApplicationState.Inactive { 
      PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo) 
     } 
     let aps = userInfo["aps"] as? [NSObject: AnyObject] 
     let alert1 = aps!["alerts"] as? String 
     let link1 = aps!["links"] as? String 
     print(userInfo) 
     print("success") 
    } 

当我调试一个接一个,它的成功,我收集的所有数据,但是我仍然缺少通知显示?

已解决第1部分 到目前为止,我设法获取数据并推送通知,但只有当我打开应用程序。代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    PFPush.handlePush(userInfo) 
    if application.applicationState == UIApplicationState.Inactive { 
     PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo) 
    } 

    let notifiAlert = UIAlertView() 

    let aps = userInfo["aps"] as? [NSObject: AnyObject] 
     let alert1 = aps!["alerts"] as? String 
     let link1 = aps!["links"] as? String 

     notifiAlert.title = alert1! 
     notifiAlert.message = link1 
     notifiAlert.addButtonWithTitle("OK") 
     notifiAlert.show() 

     print(userInfo) 
     print("success") 

} 

我使用本地通知技巧,但如何在不使用应用程序时弹出通知?

回答

3

您必须添加自定义的信息是这样的:

{"aps": {"alerts" : "test", 
     "sound": "default"}, 
"name": "Steven", 
"age": "32" 
} 

并解析它是这样的:

let aps = userInfo["aps"] as? [NSObject: AnyObject] 
let msg = aps!["alert"] as? String 
let name = userInfo["name"] as? String 
print(name) 

编辑: 你必须检查两个功能的推送通知离子UIApplicationDelegate

首先。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // check for push message 
     if let launchOptions = launchOptions { 
      let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject: AnyObject] 
      let aps = userInfo["aps"] as? [NSObject: AnyObject] 
      let msg = aps!["alert"] as? String 
      let name = userInfo["name"] as? String 
      print(name) 
     } 
} 

二。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    let aps = userInfo["aps"] as? [NSObject: AnyObject] 
    let msg = aps!["alert"] as? String 
    let name = userInfo["name"] as? String 
    print(name) 
} 
+0

我应该在哪里添加自定义信息?如果我添加自定义信息,我的数据是否会被这些数据取代?例如:我的网络JSON是“史蒂文”,它会被“测试”取代? –

+0

我编辑了我的答案。您可以添加您的自定义信息。 @Steventan –

+0

好的,但我有一个问题,我没有“名称”和“年龄”,我可以不使用该数据的权利? (根据我的JSON)@Soohwan公园 –

0

您应该将自定义数据添加到有效负载的顶层,而不是aps对象。

与此类似,比如:

{ 
    "aps": { 
     "alerts" : "test", 
     "sounds": "default" 
    }, 
    "links": "", 
} 
+0

但我收到的JSON是动态的,取决于我通过我的网站发送的内容。如果我设置在我的playload的顶部,它会变成静态的权利? –

+0

哦,我忘了一些东西,不适当更新它。 @jcaron –

+0

@Steventan它怎么会变成静态的?它是你的有效载荷的一部分。 – jcaron