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

后端 未结 3 984
挽巷
挽巷 2020-12-13 01:09

My app allows remote push notifications to a user. How do I enable it to be opened in a specific view controller when the user taps on the push notification? I want the app

相关标签:
3条回答
  • 2020-12-13 01:36

    In the AppDelegate, you will get a delegate callback "didFinishLoading" or "didReceivePushNotification" methods (based on your app is in background or foreground). In that method get the top most view controller's instance, then create the specific view controller that you want to show and present/push from top most view controller.

    0 讨论(0)
  • 2020-12-13 01:42
     UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (notification)
        {
            [self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
        }
    
    0 讨论(0)
  • 2020-12-13 01:50

    To do this you need to set an identifier for each ViewController that your app may be opened with, and then check the payload in the launchOptions argument of application:didFinishLaunchingWithOptions: in your AppDelegate Here are the steps to doing this:

    1. In your PFPush, use setData to add a key to your payload with the identifier: notification.setData(["alert":"your notification string", "identifier":"firstController"])

    2. Set the identifier on each ViewController by selecting it and changing the following values

    1. Make your Push Notification send the storyboard ID in its payload with the key identifier
    1. Check for the ID in application:didFinishLaunchingWithOptions: by adding the following at the end of the function:
    if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, identifier = payload["identifier"] as? String {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier(identifier)
        window?.rootViewController = vc
    }
    
    0 讨论(0)
提交回复
热议问题