从那里使用推送通知管理器需要重定向到特定的视图控制器

时间:2019-11-20 11:02:10

标签: swift

我试图从推送通知管理器中查看特定的viewcontroller,但是我无法从该管理器的快速页面访问任何UIViewcontroller。需要有关此解决方案的帮助

这是我的代码:

PushNotificationManager类:NSObject,MessagingDelegate,UNUserNotificationCenterDelegate {

var window: UIWindow?

override init() {
    super.init()

}

func registerForPushNotifications() {
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM)
        Messaging.messaging().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
    }
    UIApplication.shared.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print(remoteMessage.appData)
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

    Messaging.messaging().shouldEstablishDirectChannel = true

    if (UserDefaults.standard.object(forKey: CommonUtil.FB_TOKEN) as? String) != fcmToken {

        print("Send fcm Token to server",fcmToken)

        UserDefaults.standard.set(fcmToken, forKey: CommonUtil.FB_TOKEN)

    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print(response)
    print("testing notification", response.notification.request.content.userInfo)
    let userInfo = response.notification.request.content.userInfo
    if let extras = userInfo[AnyHashable("gcm.notification.extras")] {
        let data = (extras as! String).data(using: .utf8)!
        do {
            if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? NSDictionary {
                print("Notification extras: \(jsonArray)")
                if let notificationExtras = Mapper<NotificationExtras>().map(JSONObject: jsonArray) {
                    let notiType = (notificationExtras.type)?.rawValue ?? ""
                    notificationPage(type: notiType)
                }
            } else {
                print("bad json")
            }
        } catch let error as NSError {
            print(error)
        }
    }

    completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("Notification received")
    completionHandler([.badge,.alert,.sound])
    registerForPushNotifications()
}

}

1 个答案:

答案 0 :(得分:0)

需要发布NotificationCenter。

let notificationType:[String: Any] = ["NOTIFICATION_TYPE":notiType]
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "OpenPushNotification"), object: nil, userInfo: notificationType)

为NotificationCenter添加观察者

NotificationCenter.default.addObserver(self, selector: #selector(OpenPushNotification), name: NSNotification.Name(rawValue: "OpenPushNotification"), object: nil)


@objc func OpenPushNotification(_ notification: NSNotification) {
   let type = notification.userInfo![AnyHashable("NOTIFICATION_TYPE")] as? String ?? ""
      if let vc = YourViewcontroller {
          self.navigationController?.pushViewController(vc, animated: true)
           }
     }