我设法让推送通知正常工作并使用FireBase转到正确的设备。但是,如果用户点击推送通知,我希望他们所有人都立即转到特定的控制器“NotificationC”,如果用户点击推送通知,它会转到名为 HomeC 我不想要。我已阅读Firebase push notification action但仍无法将其转到该控制器这里是我的代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted)
{
UIApplication.shared.registerForRemoteNotifications()
Messaging.messaging().delegate = self as? MessagingDelegate
}
else{
//Do stuff if unsuccessful...
}
})
application.registerForRemoteNotifications()
NotificationCenter.default.addObserver(self, selector:
#selector(tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
FirebaseApp.configure()
return true
}
func tokenRefreshNotification(notification: NSNotification) {
// NOTE: It can be nil here
let login = LoginC()
login.getDeviceToken(id: MYID)
newDeviceToken = true
}
// The callback to handle data message received via FCM for devices running iOS 10 or above.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
let homeC = HomeC()
homeC.getMyNotifcations()
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}
//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let homeC = HomeC()
homeC.getMyNotifcations()
completionHandler()
}
上面的代码
let homeC = HomeC()
homeC.getMyNotifcations()
本质上是一个segue,应该让用户进入我的Notification Controller,这就是我完成它的方式
func getMyNotifcations() {
let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationC") as! NotificationC
Popup.notificationCount = Int(notificationCount!)
self.addChildViewController(Popup)
Popup.view.frame = self.view.frame
self.view.addSubview(Popup.view)
Popup.didMove(toParentViewController: self)
}
当我将 getMyNotifcations 放入IBAction时,它可以正常工作。再次,我只是希望用户在点击通知时转到NotificationC控制器,任何建议都会很棒。我还在AppDelegate中为所有这些函数设置了一个断点,并且没有一个在点击时触发。
答案 0 :(得分:2)
看起来你的视图控制器没有正确实例化,你只是在创建类的对象。
试试这个,也可能会增加延迟。
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeC = storyboard.instantiateViewController(withIdentifier: "YOUR_VIEWCONTROLLER_IDENTIFIER_IN_STORYBOARD") as? HomeC
if homeC != nil {
homeC!.view.frame = (self.window!.frame)
self.window!.addSubview(homeC!.view)
self.window!.bringSubview(toFront: homeC!.view)
homeC.getMyNotifcations()
}