Issue
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();
}
}
Solution
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 when app closed. For more information visit this ticket.
Answered By - R15
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.