Remote Notification - `Response.IsDefaultAction` when app is inactive

gtodd876gtodd876 Member ✭✭

When the user taps on a remote notification, I am pushing a specific detail view controller on my Home NavController or selecting a specific TabBarController index based off of some custom keys in the payload of the remote notification. This is all working great when the app in the background.

public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        int id;
        if (response.IsDefaultAction)
            var userInfo = response.Notification.Request.Content.UserInfo;

            topicValue = userInfo.ObjectForKey(new NSString("topic")).ToString();
            string linkType = userInfo.ObjectForKey(new NSString("linktype")).ToString();
            string linkPost = userInfo.ObjectForKey(new NSString("linkpage")).ToString();

            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController as UITabBarController;

            // .. a bunch of conditional logic to decide which view controller to push after the app comes back to the foreground 
        var homeNavController = vc.ChildViewControllers[0] as UINavigationController;
            homeNavController.PushViewController(detailViewController, true);
            vc.SelectedIndex = 0;

This works great when the app in the background.

Before I added this code, when the app was inactive, tapping on the remote notification would cause the app to open up normally to the home view controller.

After adding that logic, that app crashes after the splash screen if the user taps a remote notification when the app is inactive.

This was confusing because after researching to topic on these forums as well as Stack Overflow it sounded as if FInishedLaunching and accessing that method's launchOptions parameter was the way to deal with to deal with the user tapping on remote notification when the app is inactive.

Microsoft's App Center crash logs also confirm that the app is crashing in the DidReceiveNotificationResponse method within my if (response.IsDefaultAction) conditional logic. Yes, the is also when the app is inactive and the user taps the notification. (IsDefaultAction)

This is tough to debug because Visual Studio for Mac's debug mode stops aas soon as you close out your app manually. At this point I have to make a code change, deploy the app to my phone, close out the app, send a remote notification, tap on it and when app crashes, then check the crash log on app center. Is there a better way?

Any help would be greatly appreciated :smile:

Sign In or Register to comment.