简体   繁体   中英

How to navigating to a specific page on Push Notification tap

I am developing firebase push notifications in Xamarin.Forms using FirebasePushNotificationPlugin . Somehow I am getting notifications for android project in Application file, along with data

CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
    //System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
    var data = p.Data; //Dictionary type
};

Based on above data how can I navigate to specific Page except MainPage() ?

This is my App.xaml.cs file in Shared project

public App()
{
    InitializeComponent();
    MainPage = new MainPage();
}

MainActivity.cs class in Android project

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        FirebasePushNotificationManager.ProcessIntent(this, Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        FirebasePushNotificationManager.ProcessIntent(this, intent);
    }
}

Edit 1 : According to @G.Hakim answer below code always opening NotificationsPage without sending notification(Except first time. Only first time opening MainPage when executing app). If I click on app it always goes to NotificationsPage instead of MainPage .

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App(false,null));

    FirebasePushNotificationManager.ProcessIntent(this, Intent);
    CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
    {
        System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
        var data = p.Data;
        DependencyService.Get<IToast>().DisplayTost("opened notifications");
        LoadApplication(new App(true, p));
    };
}

App.xaml.cs

public App(bool hasNotification = false, object notificationData = null)
{
    if (hasNotification)
    {
        MainPage = new NotificationsPage();
    }
    else
    {
        MainPage = new MainPage();
    }
}

It's actually very simple you can do it using some trickery:

  • First, call the MainActivity from your notification so that you can use the existing xamarin forms loading functionality.

  • Then maintain a bool on your App class constructor something like this:

     public App(bool hasNotification=false,object notificationData=null) { if(hasNotification) {//case where you receive a notification} // call your normal functionality here else {//case where you do not recieve a notification} // notification functionality here } 
  • Now here is the trick that steals the show, When you receive a notification in your MainActivity pass the bool as true in the load app method and pass the data if anything is present in the notification.Something like this:

     LoadApplication(new App(true,*yourData*)) 

Note: I have defined the optional parameter in the constructor so you can even use the empty app constructor to make it work;

Update:

private bool IsNotification=false;
private object NotificationData;
   protected override void OnCreate(Bundle savedInstanceState)
 {
   TabLayoutResource = Resource.Layout.Tabbar;
   ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    FirebasePushNotificationManager.ProcessIntent(this, Intent);    
    CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
    {
       System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
       NotificationData = p.Data;
       IsNotification=false;
       DependencyService.Get<IToast>().DisplayTost("opened notifications");           
    };
   if(IsNotification)
    LoadApplication(new App(IsNotification,NotificationData)); 
   else
    LoadApplication(new App());
 }

This issue has been solved by sending color key from notification payload .

CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
    if (p.Data.ContainsKey("color"))
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new NotificationsPage()
            {
                BackgroundColor = Color.FromHex($"{p.Data["color"]}")
            });
        });
    }
};

Note: Above code is working when app open & in background. Not working for foreground. For more information visit this ticket .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM