Push Notification -didFinishLaunchingWithOptions

Viewed 25690

When I send a push notification and my app is open or in the background and I click on the push notification, my application redirects to PushMessagesVc viewController (as intended)

I use the code as below for this:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self.window.rootViewController presentViewController:pvc
                                                 animated:YES
                                               completion:NULL];
}

There is no problem in the code/scenario above but if the application is closed and I click on a push notification, the application does not redirect my PushMessagesVc viewController in this case & the application stays on the main screen.

For the 2nd scenario, I use the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    sleep(1);
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;

    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
        [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
        return YES;
    }
    return YES;
}

But in this case, the PushMessagesVc does not appear.

5 Answers

Swift 5

I write my notification to var "notification" in didFinishLaunchingWithOptions, and if "notification" != nil started my push in applicationDidBecomeActive, after that removed him ( = nil)

class AppDelegate: UIResponder, UIApplicationDelegate {

var notification: [AnyHashable : Any]? = nil

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

    registerForPushNotifications()

    //write notification to var
    if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {
        self.notification = launchOptions![UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
    }

    return true
}

func applicationDidBecomeActive(_ application: UIApplication) {
    if notification != nil {
    //for launch notification after terminate app
        NotificationCenter.default.post(name: Notification.Name(rawValue: "startPush"), object: nil)
        notification = nil
    }
}
Related