这是我的ReceiveRemoteNotification代码。
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")
}我试图从我的web上接收JSON数据,我的JSON看起来如下:
{"aps":{"alerts":"3","links":"","sounds":"default"}}我确实收到了Json数据并将数据转到视图中。使用此代码:
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")
}但是,我只在使用应用程序时才收到通知,但如果不使用,则无法接收通知。但我的手机震动或发出声音,但没有留言。
我是不是漏了什么东西?谢谢!
添加了这里是我的完整代码http://pastebin.com/mkFiq6Cs
编辑
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
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
notifiAlert.title = alert1!
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
print(userInfo)
print("success")
}因此,我必须添加以下代码:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if let userInfo = notification.userInfo {
let aps = userInfo["aps"] as? [NSObject: AnyObject]
let alert2 = aps!["alerts"] as? String
let link2 = aps!["links"] as? String
print("didReceiveLocalNotification: \(aps)")
print("didReceiveLocalNotification: \(alert2)")
print("didReceiveLocalNotification: \(link2)")
}
}这是在didFinishLaunchingOption:
if let options = launchOptions {
if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
if let userInfo = notification.userInfo {
let customField1 = userInfo["CustomField1"] as! String
// do something neat here
}
}
}解决了
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
let notifiAlert = UIAlertView()
if let aps = userInfo["aps"] as? [NSObject: AnyObject],
let alert1 = aps["alert"] as? String,
let content1 = aps["content-available"] as? String,
let link1 = userInfo["links"] as? String {
notifiAlert.title = alert1
notifiAlert.message = link1
notifiAlert.addButtonWithTitle("OK")
notifiAlert.show()
}
print(userInfo)
print("success")
completionHandler(UIBackgroundFetchResult.NewData)
}这工作得很好。好了!感谢@tskulbru
发布于 2016-01-02 12:15:18
这是因为只有在应用程序处于活动状态时才调用application(_:didReceiveRemoteNotification:)。它不能处理应用程序非活动/后台远程通知。
如果当远程通知到达时,应用程序没有运行,则该方法启动应用程序,并在启动选项字典中提供适当的信息。应用程序不调用此方法来处理远程通知。相反,
application:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:方法的实现需要获得远程通知有效负载数据并做出适当的响应。 资料来源:ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:
如前所述,如果application:didReceiveRemoteNotification应该能够处理后台通知,它需要有额外的实现。
如果你只想用一种方法来为你处理所有的好东西,并且没有一堆不同的方法来实现基本相同的东西,那么苹果提供了一个名为application:didReceiveRemoteNotification:fetchCompletionHandler:的方法来为你处理这一切。
您需要实现application:didReceiveRemoteNotification:fetchCompletionHandler:方法而不是这个方法来处理后台通知。这将处理后台任务,然后您可以在完成后(.NoData / .NewData / .Failed)使用appropiate调用该are。
如果希望在应用程序处于活动状态时收到远程通知时出现本地通知,则需要在上述方法中创建该通知。如果您想在应用程序处于活动状态时显示一个警报视图,您还需要处理该方法中的两个场景。我的建议是改用application:didReceiveRemoteNotification:fetchCompletionHandler:,并在那里实现所有东西。
所以在你的情况下,我会这样做:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
} else {
let notifiAlert = UIAlertView()
if 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")
}
completionHandler(UIBackgroundFetchResult.NewData)
}我还没有测试代码,但是应该是这样的,而不会有太多的更改。我没有使用Parse,所以我不知道PFPush.handlePush(userInfo)到底做了什么,您需要检查文档。
看来你的apns负载也错了。请参阅ref/doc/uid/TP40008194-CH107-SW1
就你而言:
{"aps":{"alert":"alert content","sound":"default", "content-available": 1}, "links":""}你有复数形式而不是单一形式。看看我给你的允许的钥匙。links密钥不存在于aps字典中,这意味着它是一个自定义密钥,需要放在字典之外。
请注意,这是标准的推送通知方式,在使用Parse时可能不正确。
https://stackoverflow.com/questions/34565495
复制相似问题