【问题标题】:Receive Custom Notification iOS swift 2.0接收自定义通知 iOS swift 2.0
【发布时间】:2015-12-29 07:55:51
【问题描述】:

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

代码 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")

}

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

【问题讨论】:

    标签: ios json xcode swift swift2


    【解决方案1】:

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

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

    【讨论】:

    • 我应该在哪里添加自定义信息?如果我添加自定义信息,我的数据会被这些数据替换吗?例如:我的网络 JSON 是“Steven”,它将被“test”替换??
    • 我已经编辑了我的答案。您可以像这样添加您的自定义信息。 @史蒂文坦
    • 好的,但我有一个问题,我没有“姓名”和“年龄”,我不能使用这些数据对吗? (根据我的 JSON)@Soohwan Park
    • 您不必添加“姓名”和“年龄”。您可以根据需要添加自定义数据,也可以将其从 JSON 中删除,或者您可以简单地从 ios 中忽略它们。 @史蒂文坦
    • 是的,我已经做到了,多亏了你,我现在可以检索 Json 数据了。但是我仍然错过了出现的通知。我已经更新了我的页面@Soohwan Park
    【解决方案2】:

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

    例如这样:

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

    【讨论】:

    • 但我收到的 JSON 是动态的,取决于我通过我的网站发送的内容。如果我设置在我的播放负载的顶部,它会变成静态的吗?
    • @Steventan 怎么会变成静态的?它是您的有效载荷的一部分。
    • 更新了答案以说明有效负载的结构。
    • 如果我把我的“链接”放在“aps”里面会错吗?还是它仍在工作但结构错误?
    • 我原以为这不会引起任何问题,但是由于您显然遇到了一些结构不正确的问题,所以请坚持将您的额外信息放在正确的位置,在 aps 之外。 aps 应该保留给 iOS 内部使用的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多