ios - 基于Notification有效负载快速显示Viewcontroller

标签 ios swift apple-push-notifications rx-swift

我收到推送通知并根据通知的内容显示 View Controller 。当 postman 手动触发通知时,我得到以下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]

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

我如何获取用户信息

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)")
                                }
                            }
                        }

                    } 
       }
 }

我如何处理显示所需 View Controller 的推送

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)
    }
}

然后我在我的家庭 VC 中实例化 Remote

第一个有效负载工作而第二个有效负载无效的任何原因以及我可以采取的任何措施来纠正该问题。

最佳答案

这一行

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

正在进入“返回”,因为在第二个元 46 上是一个 int 而不是字符串(请注意,它没有引号。要修复它,您可以尝试

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

关于ios - 基于Notification有效负载快速显示Viewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58304843/

相关文章:

macos - 将 struct ProcessSerialNumber 转换为 UnsafePointer<Void>

swift - 如何在 Swift 中共享流位置 betwixt 函数?

ios - 如何使用 Gimbal Manager 门户让推送通知适用于 ios?我已经完成了文档中的所有必要步骤。仍然没有运气

ios - iOS下载自定义推送通知声音

ios - GCM iOS 未收到推送通知

ios - sectionHeader 中的自动调整字体大小

swift - 请求正文中的 HTTP POST "/"字符问题 -Swift

iOS:UICollectionView 上的单独部分背景颜色

ios - AF网络 block 和提要

iphone - 获取经过验证的商家paypal邮箱ID