ios - 接收自定义通知 iOS swift 2.0

标签 ios json xcode swift swift2

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

代码 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),结果是我得到了上面的所有数据,但是没有通知。

我想我缺少一些转换数据的代码?

信息

对于具体信息,我尝试通过订阅“ channel ”接收来自 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")

}

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

最佳答案

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

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

关于ios - 接收自定义通知 iOS swift 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34506872/

相关文章:

android - MonoTouch + MonoDroid : Can project files live in same folder?

ios - 在iOS上使用Wordpress进行本地开发时,资源加载失败

javascript - 使用 KnockoutJS 从 API 加载字段后,将字段添加到 observableArray

java - 尝试使用 java 运行 sql 查询时出现 406 错误

xcode - Ionic build ios 不适用于 xcode 8 beta

ios - 如何让一个 UIButton、UILabel 和 UITextField 控制所有输入

ios - NSNotificationCenter 观察者未收到通知

javascript - 更新 setTimeout 问题中的 JSON 对象

objective-c - 如何从在线目录加载 UIImage 数组中的图像并将其存储在应用程序中

ios - XCode 4.2 story builder 的 "Defines Context"和 "Provide Context"是什么意思?