gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:33:40 25 4
gpt4 key购买 nike

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

代码 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com