'how to open an spesific form(ui) when get firebase data message in xamarin
I am try to to open a xamarin form when i get a spesific message from firebse . i can make a simple notification with a a spesific title and then my app should react for that notification . i tried to do it and it doesn't work .I am trying it on android platform and the form which i want open is locatated at cross platform package
my code
namespace DT.Samples.Agora.Cross.Droid
{
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyMessagingService:FirebaseMessagingService
{
private readonly string NOTIFICATION_CHANNEL_ID="noty";
public override void OnMessageReceived(RemoteMessage p0)
{
base.OnMessageReceived(p0);
if (p0.GetNotification().Title.Equals("hey"))
{
Log.Debug("succes", "noty: " + p0.Ttl);
}
if (!p0.Data.GetEnumerator().MoveNext())
{
SendNotidication(p0.GetNotification().Title, p0.GetNotification().Body);
}
else
{
SendNotidication(p0.Data);
}
}
private void SendNotidication(IDictionary<string, string> data)
{
string title, body;
data.TryGetValue("title", out title);
data.TryGetValue("body", out body);
SendNotidication(title, body);
}
private void SendNotidication(string title, string body)
{
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification Channel",
Android.App.NotificationImportance.Default
);
notificationChannel.Description = "EDMTDev Channel";
notificationChannel.EnableLights(true);
notificationChannel.LightColor = Color.Blue;
notificationChannel.SetVibrationPattern(new long[] { 0, 1000, 500, 1000 });
notificationManager.CreateNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificaionBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificaionBuilder.SetAutoCancel(true)
.SetDefaults(-1)
.SetWhen(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds())
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(Resource.Drawable.belicon)
.SetContentInfo("info");
notificationManager.Notify(new Random().Next(), notificaionBuilder.Build());
//dGNlRe7p73w:APA91bFqU7GxoVfw1ljNTGrvgpMMEs-Y7nh9W3k1Uecns2I88BUTQrSdrr3nyl0eNFHeKbWRn6ZFS0e_VNcBWSW0L-wYKv81tPddLyTaH_6IHPE6oWlKcdaMn_zsiLiaH8aJXch-xnRj
}
}
}
Solution 1:[1]
If you are running a Xamarin.Forms app, you can call MyCrossPlatformProject.App.Current.MainPage.Navigation.PushModalAsync(new MyPage());
This will call the App's running instance and it will be able to "open" the page you want by pushing it to the navigation stack. This will only work if App has been initialized. Is this what you are looking for?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Electric Potato |
