Though this is a repeated question, still I didn't get the exact answer for this.
I am trying this on Pixel 2 device.
- When the app is in foreground/background - Getting the notification
- when the app is swipe out from the background stack, am getting a message says "My App has stopped"
- The next if I try to get the notification, noting is coming up. Not receiving the stopped dialog also.
If any reference from Google, please let me know.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
CrossCurrentActivity.Current.Activity.RunOnUiThread(() => Android.Widget.Toast.MakeText(CrossCurrentActivity.Current.Activity, "Notification received", Android.Widget.ToastLength.Long).Show());
}
public override void HandleIntent(Intent intent)
{
try
{
if (intent.Extras != null)
{
var builder = new RemoteMessage.Builder(typeof(MyFirebaseMessagingService).Name);
foreach (string key in intent.Extras.KeySet())
{
builder.AddData(key, intent.Extras.Get(key).ToString());
}
this.OnMessageReceived(builder.Build());
}
else
{
base.HandleIntent(intent);
}
}
catch (Exception)
{
base.HandleIntent(intent);
}
}
}
My Payloads
Type 1
{
"message":{
"token":"<MyDevice token>",
"notification":{
"body" : "Someone has escaped the jail",
"title" : "Jail escape alert"
}
}
}
Type 2
{
"message":{
"token":"<My Device token>",
"notification":{
"body" : "Someone has escaped the jail",
"title" : "Jail escape alert"
},
"data" : {
"Eventid" : "911",
"Longmessage" : "A super-secret message",
"id" : "007"
},
"android":{
"priority":"high"
}
}
}
For both am not receiving the notification. Even the Firebase documentation also suggesting the same way. https://firebase.google.com/docs/cloud-messaging/concept-options