Adeptus Mechanicus
Adeptus Mechanicus

Reputation: 171

Onesignal NotificationOpened doesn't work in ios app

I try to work with Onesignal Notifications in my ios app, created in Visual Studio with Xamarin. I want to make application open ViewController when user tap on notification, but it doesn't work. When user tap notification, app opens ViewController which was active when user application go to background. Hope somebody show, where I made mistake Here the code:

public class AppDelegate : UIApplicationDelegate
{
    public static UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);
    public override UIWindow Window {
        get;
        set;
    }

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        OneSignal.Current.StartInit("mykey")
            .InFocusDisplaying(OSInFocusDisplayOption.None)
            .HandleNotificationOpened(HandleNotificationOpened)
            .EndInit();
        OneSignal.Current.IdsAvailable(IdsAvailable);            
        return true;
    }
    void IdsAvailable(string userID, string pushToken)
    {
        GlobalUserInfo._token = userID;
    }
    private static void HandleNotificationOpened(OSNotificationOpenedResult result)
    {
        OSNotificationPayload payload = result.notification.payload;
        Dictionary<string, object> additionalData = payload.additionalData;

        if (additionalData != null)
        {
            UINavigationController NavigationController = (UINavigationController)Storyboard.InstantiateInitialViewController();
            NotificationsViewController notificationsController = Storyboard.InstantiateViewController("NotificationsViewController") as NotificationsViewController;
            NavigationController.PushViewController(notificationsController, true);
        }
    }
}

NotificationsViewController is simple list of all notification user received.

UPDATED As I noticed, function HandleNotificationOpened does not work in iOS.

UPDATED Error was found. I forgot, that I use OneSignal Init in other place. When I delete duplicate, HandleNotificationOpened begins work correctly.

Upvotes: 0

Views: 455

Answers (1)

Ax1le
Ax1le

Reputation: 6641

You should find current root ViewController instead of creating a new instance. So please modify like this:

if (additionalData != null)
{
    UINavigationController NavigationController = (UINavigationController)Window.RootViewController;
    NotificationsViewController notificationsController = Storyboard.InstantiateViewController("NotificationsViewController") as NotificationsViewController;
    NavigationController.PushViewController(notificationsController, true);
}

Moreover, if the root ViewController is a UITabBarController, please present this view controller(NotificationsViewController).

Upvotes: 1

Related Questions