How to handle notification click on Xamarin form ios project

When my Xamarin ios app is in background and I am clicking on the Push notification it is always opening the page i.e in background. Please let me know how I can open another app page on clicking push notifications. when I am clicking on notification public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler)
{
}
event is called but I am not able to redirect to another app page.

Posts

  • GurinderBaggaGurinderBagga USMember ✭✭

    Hello @BalaRaju

    Thanks for the solution. You saved my day :)

  • maheshdatrikamaheshdatrika USMember

    @BalaRaju,

    Thank you very much, It's working like a charm

  • GurinderBaggaGurinderBagga USMember ✭✭

    @BalaRaju

    If the app is in Background state (i.e we have closed all the app pages from the background) then how to navigate to a particular page.

    Thanks
    Gurinder

  • MichaelLouwMichaelLouw USMember

    EASY FIX!!!
    In your delegate.cs and in your mainactivity.cs you are initializing your your App (app.cs) when your can the just save an instance of your app in the mainactivity and delegate classes respectively and then just call any method to navigate or do whatever with the clicked notification.

  • DeepakDYDeepakDY INMember ✭✭✭

    Hello Developers,

    this solution not work for me in background mode.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    @DeepakDY We need more information on what you tried and the results.
  • DeepakDYDeepakDY INMember ✭✭✭

    Hello @JamesLavery

    I tried this
    nothing get any errors but its open first page not open set page when click notification

    ` public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate//, IUNUserNotificationCenterDelegate, IMessagingDelegate
    {
    //
    // This method is invoked when the application has loaded and is ready to run. In this
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //
    public GetCurrentAddress currentAddress;
    public MyLocation Manager { get; set; }
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
    global::Xamarin.Forms.Forms.Init();

            Manager = new MyLocation();
            Manager.StartLocationUpdates();
            Manager.LocationUpdated += HandleLocationChanged;
            currentAddress = new GetCurrentAddress();
            Rg.Plugins.Popup.Popup.Init();
            ImageCircleRenderer.Init();
    
            global::Xamarin.FormsMaps.Init();
            DeviceHelper.iOSDevice = UIDevice.CurrentDevice;
    
            FirebasePushNotificationManager.Initialize(options);
    
            currentAddress.ReverseGeoCodeLatLonAsync(App.CurrentLattitude, App.CurrentLongitude);
            return base.FinishedLaunching(app, options);
        }
    
        public void HandleLocationChanged(object sender, LocationUpdatedEventArgs e)
        {
            CLLocation location = e.Location;
            App.CurrentLongitude = location.Coordinate.Longitude;
            App.CurrentLattitude = location.Coordinate.Latitude;
            Console.WriteLine("foreground updated");
        }
    
        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {  
           FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken, FirebaseTokenType.Sandbox);
        }
    
        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
      Action<UIBackgroundFetchResult> completionHandler)
        {
    
            try {
                ProcessNotification(userInfo, false);
            }
            catch (Exception ex)
            {
                //LogInfo.ReportErrorInfo(ex.Message, ex.StackTrace, "AppDelegate-DidReceiveRemoteNotification");
            }
        }
    
    
        void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
        {
            // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
            if (null != options && options.ContainsKey(new NSString("aps")))
            {
                //Get the aps dictionary
                NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
    
                string alertString = string.Empty;
                string paramString = string.Empty;
    
                if (aps.ContainsKey(new NSString("alert")))
                    alertString = (aps[new NSString("alert")] as NSString).ToString();
    
                if (aps.ContainsKey(new NSString("param")))
                    paramString = (aps[new NSString("param")] as NSString).ToString();
    
                if (!fromFinishedLaunching)
                {
                    //Manually show an alert
                    if (!string.IsNullOrEmpty(alertString))
                    {
                        UIAlertView avAlert = new UIAlertView("Awesome Notification", alertString, null,
                            NSBundle.MainBundle.LocalizedString("Cancel", "Cancel"),
                            NSBundle.MainBundle.LocalizedString("OK", "OK"));
    
                        avAlert.Clicked += (sender, buttonArgs) =>
                        {
                            if (buttonArgs.ButtonIndex != avAlert.CancelButtonIndex)
                            {
                                if (!string.IsNullOrEmpty(paramString))
                                {
                                    App.Current.MainPage = new NavigationPage(new SelectCategoryView());//AppliedJobDetails(null, notificationMessage, NotificationId);
                                    //App.Current.MainPage = new NavigationPage(new );
                                }
                            }
                        };
    
                        avAlert.Show();
                    }
                }
            }
        }
    
    private void debugAlert(string title, string message)
        {
            var alert = new UIAlertView(title ?? "Title", message ?? "Message", null, "Cancel", "OK");
            alert.Show();
        }
    
    
    
        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
            Console.WriteLine("I get called when in foreground");
        }
    }`
    
Sign In or Register to comment.