gpt4 book ai didi

ios - 如何保存 pushNotification 附带的两个项目

转载 作者:行者123 更新时间:2023-11-28 11:30:49 25 4
gpt4 key购买 nike

1)当通知到来时,我需要保存来自 AnyHashable("data") "amount"和 "inBalance"的数据吗?

2)来自服务器的两次推送,第一次推送“消息”,第二次推送“nil message body”。当出现 nil 消息时,我需要静音吗?我怎样才能保存它?我正在使用 xCode 10.2.1

我的 pushNotification.swift 代码如下:

enum PillikanRemoteNotification:Int {
case empty
case notification = 2
case news = 3
}

class PushManagerNotification {
func convert(with value: [AnyHashable: Any], and state: PushNotificationAction) {
guard let json = value as? [String: AnyObject], !json.isEmpty else { return }
guard let jsonString = value["data"] as? String, !jsonString.isEmpty else { return }
guard let jsonData = jsonString.data(using: .utf8) else { return }
guard let rawDictionary = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) else { return }
guard let dictionary = rawDictionary as? [String: AnyObject] else { return }
guard let actionType = dictionary["action"] as? String, let action = Int(actionType) else { return }
guard let notificationType = PillikanRemoteNotification(rawValue: action) else { return }
guard let messageBody = dictionary["msg"] as? [String: AnyObject] else { return }

if state == .show {
showBadges(dictionary: messageBody)
return
}

switch notificationType {
case .notification:
showNotification(dictionary: messageBody)
break
default:
break;
}
}

private func showBadges(dictionary: [String: AnyObject]) {
guard let badgeMessage = dictionary["badges"] as? [String: AnyObject] else { return }
guard let badges = Badges(JSON: badgeMessage) else { return }
UIApplication.shared.notificationBadgesForNotification = badges.notifications
UIApplication.shared.notificationBadgesForNews = badges.news
UIApplication.shared.applicationIconBadgeNumber = badges.news + badges.notifications
NotificationCenter.default.post(name: .badges, object: badges)
}

private func showNotification(dictionary: [String:AnyObject]) {
if let message = NotificationEntity(JSON: dictionary) {
NotificationCenter.default.post(name: .notification, object: message);
}
}

extension Notification.Name {
static let empty = Notification.Name("empty");
static let notification = Notification.Name("notification");
static let news = Notification.Name("news");
static let badges = Notification.Name("badges")
}

最佳答案

对于静默通知,有两个标准:

1.) 有效负载的 aps 字典必须包含值为 1 的内容可用键。

2.) 有效负载的 aps 字典不得包含警报、声音或角标(Badge)键。

例子

{ “应用程序”:{ “可用内容”:1 }, “cusomtkey1”:“栏”, “cusomtkey2”:42

关于ios - 如何保存 pushNotification 附带的两个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765993/

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