接收自定义通知iOS Swift 2.0
我试图通过parsing网站从我的网站收到我的自定义通知。
代码didReceiveRemoteNotification:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { PFPush.handlePush(userInfo) if application.applicationState == UIApplicationState.Inactive { PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)}
这是我从parsing网站收到的JSON:
[aps: { alert = "test adirax"; sound = default; }]
它适合我,并且通知显示出来。 但是,当我试图从我的网站推送数据,通知不能显示/popup。
这是我的JSON看起来像:
{"aps": {"alerts" : "test", "links": "", "sounds": "default"}}
我试图打印(userInfo),结果是我得到像上面的所有数据,但没有notif。
我想我错过了一些代码来转换数据?
信息
对于具体的信息,我试图通过订阅“渠道”接收来自parse.com的通知。 所以这不是一个本地通知或其他。
添加代码(根据我的JSONtypes)
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") }
当我一一debugging,其成功我收集所有的数据,但我仍然缺less通知显示了?
已解决第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") }
我使用本地通知技巧,但是如何在不使用应用程序时popup通知?
你必须添加这样的自定义信息:
{"aps": {"alerts" : "test", "sound": "default"}, "name": "Steven", "age": "32" }
并parsing它是这样的:
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": "", }