gpt4 book ai didi

ios - UILocalNotification 没有出现

转载 作者:行者123 更新时间:2023-11-29 01:21:18 25 4
gpt4 key购买 nike

这是我的 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")

}

我试图从我的网站接收 JSON 数据,我的 JSON 看起来像:

{"aps":{"alerts":"3","links":"","sounds":"default"}}

确实收到了Json数据,把数据转给view。使用此代码:

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

最佳答案

这是因为 application(_:didReceiveRemoteNotification:) 仅在应用程序处于事件状态时才会被调用。它无法处理应用程序非事件/后台远程通知。

If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.

Source: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:

如上所述,application:didReceiveRemoteNotification 需要额外的实现才能处理后台通知。

如果您只想要一个方法来为您处理所有好事,并且没有一堆不同的方法来实现基本相同的事情,Apple 提供了一个名为 application:didReceiveRemoteNotification:fetchCompletionHandler 的方法: 为您处理这一切。

您需要实现 application:didReceiveRemoteNotification:fetchCompletionHandler: 方法而不是这个方法来处理后台通知。这会处理后台任务,然后您可以在完成时使用适当的枚举调用 completionHandler (.NoData/.NewData/.Failed )。

如果您希望在应用程序处于事件状态时收到远程通知时显示本地通知,则需要使用上述方法创建它。如果您想在应用程序处于事件状态时显示警报 View ,您还需要在该方法中处理这两种情况。我的建议是改用 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 有效负载也错误。参见 https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

就您而言:

{"aps":{"alert":"alert content","sound":"default", "content-available": 1}, "links":""}

您使用的是复数形式而不是单一形式。查看我为允许的 key 提供的 url。 aps 字典中不存在 links 键,这意味着它是一个自定义键,需要放置在字典之外。

请注意,这是标准的推送通知方式,在使用 Parse 时可能不正确。

关于ios - UILocalNotification 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565495/

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