[英]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.