1)当收到通知时,我需要保存来自任何哈希(“data”)“amount”和“inBalance”的数据?
2)来自服务器的两次推送,第一次推送带有“消息”,第二次推送“无消息体”,当“无消息”出现时,我需要静音吗?
我怎样才能挽救它?
我用的是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")
}

最佳答案

ios - 如何保存推式通知随附的两个项目-LMLPHP
在这里你可以看到,我是如何从任意哈希表中获取令牌,然后从中获取特定值的。
试试这个代码。它会帮你的。

if(response.result.isSuccess){
       let data = response.result.value
          print(data!)
            if (data?.contains("access_token"))!{
                var dictonary:NSDictionary?
                    if let data = data!.data(using: String.Encoding.utf8) {
                       do {
                            dictonary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] as NSDictionary?

                                UserDefaults.standard.set(dictonary!["access_token"]!, forKey: "token")


                            }catch let error as NSError {
                                print(error)
                            }
                        }
                     }
                   }

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

10-11 19:56