0

I am super new in Xamarin.iOS and I tried to find my problem and how to handle notifications but all what I got is showing an alert examples.

Just like in whatsapp once you touch the notification that you get it would open a certain ViewController with the data, I would like to do something similar. Once i get a remote notification with a text on it , I want to show that text in the main ViewController. How to do that ?

I found some similar questions but they are either in Swift or Objective-c

here, here and here

Community
  • 1
  • 1
Ahmad ElMadi
  • 2,507
  • 21
  • 36

1 Answers1

1
    //when you app in background and user click your notification
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        var userInfo = launchOptions.ValueForKey(UIApplication.LaunchOptionsRemoteNotificationKey) as NSDictionary;
        if (userInfo != null)
        {
            var message = userInfo.ValueForKey(new NSString("yourTextKey")) as NSString;
            var vc = new MessageViewController();
            vc.TextView.Text = message;
            Window.RootViewController = vc;

        }
        else
        {
            Window.RootViewController = new OtherRootViewController();
        }
        Window.MakeKeyAndVisible();
        return true;
    }

    //when you app in foreground
    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
    {
        if (userInfo != null)
        {
            var message = userInfo.ValueForKey(new NSString("yourTextKey")) as NSString;
            var vc = new MessageViewController();
            vc.TextView.Text = message;
            Window.RootViewController.PresentViewController(vc, true, null);
        }
    }
sunyt
  • 309
  • 3
  • 7