根据通知负载 swift 显示 Viewcontroller

Displaying Viewcontroller based on Notification payload swift

我正在收到推送通知并根据 notification.When 的内容显示 viewcontroller 通知是邮递员手动触发的,我收到以下内容 userInfo

    [AnyHashable("google.c.a.e"): 1, AnyHashable("gcm.notification.type"): new_property, AnyHashable("gcm.notification.meta"): {"property_id":"3"}, AnyHashable("gcm.message_id"): 1570614460795417, AnyHashable("aps"): {
        alert =     {
            body = "A new property has been published is in your area";
            title = "New Property";
        };
    }]
// CASTING AS [String: Any]
    ["google.c.a.e": 1, "gcm.notification.meta": {"property_id":"3"}, "gcm.notification.type": new_property, "aps": {
        alert =     {
            body = "A new property has been published is in your area";
            title = "New Property";
        };
    }, "gcm.message_id": 1570614460795417]

这反过来会起作用并显示所需的屏幕。但是每当用户操作触发相同的通知时,我都会收到此 userInfo

    [AnyHashable("gcm.notification.meta"): {"property_id":46}, AnyHashable("gcm.notification.type"): new_property, AnyHashable("body"): A new property has just been listed in your area by Ebenezer Kilani, AnyHashable("title"): New property Listing, AnyHashable("type"): new_property, AnyHashable("meta"): {"property_id":46}, AnyHashable("aps"): {
        alert =     {
            body = "A new property has just been listed in your area by Ebenezer Kilani";
            title = "New property Listing";
        };
    }, AnyHashable("gcm.message_id"): 1570550088749025, AnyHashable("google.c.a.e"): 1]
// CASTING AS [String: Any]
    ["meta": {"property_id":46}, "type": new_property, "title": New property Listing, "gcm.message_id": 1570550088749025, "gcm.notification.meta": {"property_id":46}, "body": A new property has just been listed in your area by Ebenezer Kilani, "aps": {
        alert =     {
            body = "A new property has just been listed in your area by Ebenezer Kilani";
            title = "New property Listing";
        };
    }, "google.c.a.e": 1, "gcm.notification.type": new_property]

第二个有效负载只是打开应用程序,但从未进入我需要的视图控制器。也就是说,它只打开主页。

我如何获取用户信息

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        let userInfo = response.notification.request.content.userInfo
        if let usrInfo = userInfo as? [String: Any] {
            extractUserInfo(userInfo: usrInfo, completion: completionHandler)
        }
        completionHandler()
    }

func extractUserInfo(userInfo: [String: Any], completion: @escaping () -> Void) {

        if let type = userInfo["gcm.notification.type"] as? String {
                    Storage.instance.savePush(type)
                    if type == "new_property" {
                        if let meta = userInfo["gcm.notification.meta"] as? String {

                            if let data = meta.data(using: .utf8) {

                                do {
                                    if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] {
                                        guard let propertyId = json["property_id"] as? String, let id = Int(propertyId) else {return}
                                        NotificationEvent.isFromNotification.accept(true)
                                        NotificationEvent.id.accept(id)

                                    } else {
                                        print("JSON is invalid")
                                    }
                                } catch {
                                    print("Exception converting: \(error)")
                                }
                            }
                        }

                    } 
       }
 }

我如何处理需要显示的推送Viewcontroller

class Remote {

    var navigationController: UINavigationController?
    let disposeBag = DisposeBag()

    @discardableResult
    init(navigationController: UINavigationController?) {
        self.navigationController = navigationController
        rxEvent()
    }

    func rxEvent() {

        NotificationEvent.isFromNotification.asObservable().subscribe(onNext: { bool in
            print("Exception converting: bool \(bool)")
            if bool {
                NotificationEvent.id.asObservable()
                    .delay(1, scheduler: MainScheduler.instance)
                    .subscribe(onNext: { int in
                    if int > 0 {
                        if Storage.instance.getPush() == "new_property" {
                            self.presentPropertyDetailVC(int)
                        } else if Storage.instance.getPush() == "new_rating" || Storage.instance.getPush() == "coverage_area" {
                            print(int)
                        }
                    }
                }).disposed(by: self.disposeBag)
                NotificationEvent.isFromNotification.accept(false)
            }
        }).disposed(by: disposeBag)
    }

    func presentPropertyDetailVC(_ uid: Int) {
        //        navigationVC.modalPresentationStyle = .formSheet
        let controller = PropertyDetailVC()
        controller.propertyId = uid
        print("Exception converting: 11 \(controller)")
        navigationController?.pushViewController(controller, animated: true)
    }
}

然后我在我家实例化RemoteVC

为什么第一个有效负载工作而第二个不工作的任何原因以及我可以做的任何纠正。

这一行

guard let propertyId = json["property_id"] as? String, let id = Int(propertyId) else {return} 

正在进入 "return" 因为在第二个元数据上 46 是一个整数而不是字符串(请注意它没有引号。要修复它你可以尝试

guard let propertyId = json["property_id"] as? String ?? String(json["property_id"] as? Int ?? 0), let id = Int(propertyId) else {return}