Open a View Controller When a iOS Push Notification Is Received

Swift - How to open specific view controller when push notification received?

When you app is in closed state you should check for launch option in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { }

and call your API.

Example:

if let option = launchOptions {
let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
if (info != nil) {
goAnotherVC()
}
}

How to Open a Notification view controller when a iOS push notification is received?

For Kill State OR Terminate State:

Define new property in AppDelegate.swift file

var isFromNotification = false

Make below changes in AppDelegate.swift and keep rest of code as it is.

if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {
isFromNotification = true
//other required code
}

Goto your Homepage.swift file

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated).

let appDelegate = UIApplication.shared.delegate as! AppDelegate
if appDelegate.isFromNotification {
appDelegate.isFromNotification = false
//Push to your notification controler without animation
}
}

Update: For Background and Foreground State

Above answer is only work for Kill state as mention in the comment by @Abu Ul Hassan

Now, Let's understand the flow about the background or foreground state.

userNotificationCenter(_:didReceive:withCompletionHandler:) method is called when a user clicks on the notification in background or foreground mode.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//other required code
//Get current navigation controller and redirect into notification screen
completionHandler()
}

How to grab current UIViewController or current UINavigationController

Open app in specific view when user taps on push notification with iOS 13 Swift 5

You can trigger a notification once you receive and the user clicks on the notification.

Along with the notification, you can pass the value which will later used to identify to which view controller you need to navigate.

Create a class which will be responsible for all the push notification navigation handling. You can name it like PushNotificationHandler. Let the PushNotificationHandler handler take care of all the logic to navigate to the view controllers.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
}
  • Get the value from the deep link using the above delegate method
  • Trigger a notification with the value
  • Handle the notification inside PushNotificationHandler class

Open a ViewController from remote notification

I created a sample project with a local notification instead of a remote notification for ease of showing the functionality but it should be as simple as setting the root view controller of the window in the app delegate didreceiveremote notification.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// Subscribe for notifications - assume the user chose yes for now
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))

return true
}


func applicationDidEnterBackground(application: UIApplication) {
//Crete a local notification
let notification = UILocalNotification()
notification.alertBody = "This is a fake notification"
notification.fireDate = NSDate(timeIntervalSinceNow: 2)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewControllerWithIdentifier("otherVC") as! OtherViewController
window?.rootViewController = otherVC;
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
//Your code here
}

`
You need to worry about managing your view hierarchy and sending anything to it that you need to send from the notification user data.

In my example, I create a local notification when you close the app that fires after a view seconds. If you then launch the app from the notification, it will open the "other view controller" which would be the "SimplePostViewController" in your case.

Also, be sure that you are registering for remote notifications in the didFinishLaunchWithOptions.

Github very simple sample : https://github.com/spt131/exampleNotificationResponse

Open specific ViewController from push notification when application closed

If you want to open vc from the closed state of your app, you can check if app was opened from click on Notification in this method of AppDelegate.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 

It looks like:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// When the app launch after user tap on notification (originally was not running / not in background)
if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {

goToEventDetails(userInfo: launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any])

}
}

Where goToEventDetails is method which instantiates VC and pushed id, and [UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] is content of Notification.

Open view controller when receiving remote Push Notification

Please try the following code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}


Related Topics



Leave a reply



Submit