[英]Receive Custom Notification iOS swift 2.0
I tried to receive my custom notification from my web through parse website. 我试图通过解析网站从我的网站收到我的自定义通知。
Code didReceiveRemoteNotification : 代码didReceiveRemoteNotification:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)}
Here is the JSON i received from parse website : 这是我从解析网站收到的JSON:
[aps: {
alert = "test adirax";
sound = default; }]
It works well for me and the notification shows up. 它适用于我,通知显示。 However when i tried to push data from my website, the notification can't shows/ pop up. 但是当我试图从我的网站推送数据时,通知无法显示/弹出。
Here is my JSON looks like : 这是我的JSON看起来像:
{"aps": {"alerts" : "test",
"links": "",
"sounds": "default"}}
I tried to print(userInfo) and the result is i get all the data like above, but there is no notif. 我试图打印(userInfo),结果是我得到了上面的所有数据,但没有通知。
I guess I'm missing some code to convert the data ? 我想我错过了一些转换数据的代码?
INFORMATION 信息
For the specific information, i tried receive notification from parse.com through subscribe "channels". 对于具体信息,我尝试通过订阅“频道”从parse.com接收通知。 so it's not a local notification or else. 所以这不是本地通知或其他。
ADD Code Added (according to my JSON type) 添加添加代码(根据我的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
}
And this : 还有这个 :
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")
}
When i debugged one by one, its success i collect all the data, however i still missing the notification showed up ? 当我逐个调试时,它的成功我收集了所有数据,但是我仍然错过了通知出现了吗?
SOLVED PART 1 So far i managed to get the data and push the notification out, but it's only when i open the application. 已解决的第1部分到目前为止,我设法获取数据并推出通知,但这只是在我打开应用程序时。 Code : 代码:
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")
}
I use a local notification trick, but how to pop up the notification when i'm not using the app ? 我使用本地通知技巧,但是当我不使用应用程序时如何弹出通知?
You have to add custom informations like this: 你必须添加这样的自定义信息:
{"aps": {"alerts" : "test",
"sound": "default"},
"name": "Steven",
"age": "32"
}
And parse it like this: 并解析它:
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let msg = aps!["alert"] as? String
let name = userInfo["name"] as? String
print(name)
EDIT: You have to check push notification on two functions of UIApplicationDelegate
编辑:您必须检查UIApplicationDelegate
两个函数的推送通知
First. 第一。
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)
}
}
Second. 第二。
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)
}
You should add your custom data to the top-level of your payload, not to the aps
object. 您应该将自定义数据添加到有效负载的顶层,而不是添加到aps
对象。
Like this, for instance: 像这样,例如:
{
"aps": {
"alerts" : "test",
"sounds": "default"
},
"links": "",
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.