user1591668
user1591668

Reputation: 2893

IOS 10 how to handle user clicking on push notification

I have managed to get push notifications working correctly and going to the right device using FireBase. However if a user clicks on the push notification I would like all of them to go to a specific Controller Called "NotificationC" right now if a user clicks on a push notification it goes to a controller called HomeC which I don't want . I have read Firebase push notification action but still can't get it to go to that controller here is my code.

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

The code above

            let homeC = HomeC()
            homeC.getMyNotifcations()

is essentially a segue which should get users to my Notification Controller and this is how I have it done

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

When I put the getMyNotifcations in a IBAction it works . Again I simply want the user to go to the NotificationC Controller when they tap on the Notification any suggestions would be great . I also put a breakpoint on all those functions in the AppDelegate and none of them fire off on tap .

Upvotes: 2

Views: 5003

Answers (1)

Umar Farooque
Umar Farooque

Reputation: 2059

It seems like your view controller is not instantiated properly, you are just making the object of the class.

Try this and maybe add delay too.

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

Upvotes: 2

Related Questions