0

I build firebase notification in my app. And it is successful working. But i want to open if notification data is equal "Second" then open SecondViewController in my app. How can i do? I use userInfo but i did not do it.

Firebase Notification Special Data: { "view" : "Second" }

This is my full appdelegate code. Thank you. I am sorry for my english.

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        application.registerForRemoteNotifications()
        requestNotificationAuthorization(application: application)
        if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {

            NSLog("[RemoteNotification] applicationState: \(applicationStateString) didFinishLaunchingWithOptions for iOS9: \(userInfo)")
            //TODO: Handle background notification
        }
        return true
    }

    var applicationStateString: String {
        if UIApplication.shared.applicationState == .active {
            return "active"
        } else if UIApplication.shared.applicationState == .background {
            return "background"
        }else {
            return "inactive"
        }
    }

    func requestNotificationAuthorization(application: UIApplication) {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
    }
}

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
    // iOS10+, called when presenting notification in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) willPresentNotification: \(userInfo)")
        //TODO: Handle foreground notification
        completionHandler([.alert])
    }

    // iOS10+, called when received response (default open, dismiss or custom action) for a notification
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) didReceiveResponse: \(userInfo)")
        //TODO: Handle background notification
        completionHandler()
    }
}

extension AppDelegate : MessagingDelegate {
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        NSLog("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)")
    }

    // iOS9, called when presenting notification in foreground
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        NSLog("[RemoteNotification] applicationState: \(applicationStateString) didReceiveRemoteNotification for iOS9: \(userInfo)")
        if UIApplication.shared.applicationState == .active {
            //TODO: Handle foreground notification
        } else {
            //TODO: Handle background notification
        }
    }
}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • If you just want to show... make it rootViewController... otherwise push it in your navigation controller... You can directly use the window 's object. Othewrise post a local NSNotification with data in your initial view controller – Naresh Sep 12 '17 at 15:20
  • Thank you for answer. Could you write example code? – Burak Gür Sep 13 '17 at 13:14
  • https://stackoverflow.com/questions/24049020/nsnotificationcenter-addobserver-in-swift and https://stackoverflow.com/questions/30956476/swift-instantiating-a-navigation-controller-without-storyboards-in-app-delegat – Naresh Sep 13 '17 at 13:22

1 Answers1

2

notification data from userinfo
inside userNotificationCenter add this code...

let str:String = (userInfo["gcm.notification.imType"] as? String)!

    switch str {
    case "FIRST":

       let rootViewController = self.window!.rootViewController as! UINavigationController

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let firstViewController = storyboard.instantiateViewController(withIdentifier: "FIRSTID") as! MessagesTableViewController

            rootViewController.pushViewController(firstViewController, animated: true)

      case "SECOND":

        let rootViewController = self.window!.rootViewController as! UINavigationController

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewController(withIdentifier: "SECONDID") as! SecondViewController

        rootViewController.pushViewController(secondViewController, animated: true)
 default:
  print("Type is something else")

}
naga
  • 397
  • 2
  • 12
  • 26