Anand
Anand

Reputation: 1959

Xamarin.ios firebase push notification click issue

I have a xamarin.forms app which implemented firebase push notifications.Currently I am playing with the ios portion. I can receive the notifications when App is in foreground, background and killed. The problem is handling the notification click.I am trying to call MessagingCenter from DidReceiveNotificationResponse method in order to open a contentpage(My Notification details page in forms).I have two doubts

  1. I am only handling the notification click from DidReceiveNotificationResponse method. I can able to handle the notification from this method. Currently I am testing on Iphone with ios 12.4.6. Is there any additional things to do for handling notifications on lower os versions? Or it is enough for all os versions?

  2. How to handle the notification click when APP is in closed or killed state?Currently the app will navigate to my specific page on all scenarios, but in killed state the app itself will close the message details page after loading and navigate to home screen automatically.How to solve this?

My DidReceiveNotificationResponse

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action
    completionHandler)
    {
        completionHandler();
        NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
        var a = userInfo[new NSString("user_notification_id")] as NSString;
        try
        {            
            var myData = JsonConvert.DeserializeObject(userInfo[new NSString("user_notification_id")] as NSString);

            if (myData != null)
            {
                Settings.NotificationID = myData.ToString();
                Settings.NCNotificationStatus = null;
            }              
        }

        catch (Exception ex)
        {

        }

        // Managing notification click here
        MessagingCenter.Send<Object>(new Object(), "iosNcnotificationTapped");
    }

My App.xaml.cs

public partial class App : Application
    {
        public string Isnotification;
        public App(bool hasNotification = false)
        {       
            InitializeComponent();
            if (hasNotification)
            {
                var navPage = new NavigationPage(new LandingPage());
                Application.Current.MainPage = navPage;
                navPage.Navigation.PushModalAsync(new ChatPage("26"));        
            }
            else
            {                         
                var splashPage = new CustomRender.TransitionNavigationPage(new SplashPage());
                MainPage = splashPage;            
            }   
                MessagingCenter.Subscribe<Object>(this, "iosNcnotificationTapped", async (sender) => {                  
                        var navPage = new NavigationPage(new LandingPage());
                        Application.Current.MainPage = navPage;
                        await navPage.Navigation.PushModalAsync(new ChatPage("26"));

                });
        }

        protected override void OnStart()
        { 

        }
        protected override void OnSleep()
        {  

        }
        protected override void OnResume()
        {   

        }

    }

The hasNotification part is for android portion. For ios it will be always false.How to solve these issues?

Upvotes: 0

Views: 567

Answers (1)

nevermore
nevermore

Reputation: 15816

C# version for the solution in this thread

In your iOS project:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{

    //if (launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]) != nil {
    //    // check the userInfo and take the needed action
    //}

    if (options != null)
    {
        NSObject result;
        if (options.TryGetValue(UIApplication.LaunchOptionsRemoteNotificationKey, out result))
        {
            //Get remoteNotification
        }
    }

    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}

Upvotes: 1

Related Questions