繁体   English   中英

接收自定义通知iOS swift 2.0

[英]Receive Custom Notification iOS swift 2.0

我试图通过解析网站从我的网站收到我的自定义通知。

代码didReceiveRemoteNotification:

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

这是我从解析网站收到的JSON:

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

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

这是我的JSON看起来像:

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

我试图打印(userInfo),结果是我得到了上面的所有数据,但没有通知。

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

信息

对于具体信息,我尝试通过订阅“频道”从parse.com接收通知。 所以这不是本地通知或其他。

添加添加代码(根据我的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")

}

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

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

{"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)
}

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

像这样,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM