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



我试图从推送通知管理器查看特定的视图控制器,但我无法从该管理器swift页面查看任何UIViewcontroller。此解决方案需要帮助

这是我的代码:

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

}

需要发布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)
}
}

最新更新