3

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

Sankarann
  • 2,625
  • 4
  • 22
  • 59
  • Can you pop your payload example in your question. We send both a notification and a data message, this usually covers all of the three scenarios. Data messages are processed in app, whereas standard notification messages can be interpreted by the system when the app is closed etc. – JoeTomks May 02 '18 at 15:34
  • [Possibly helpful post](https://stackoverflow.com/a/39505298/4625829) – AL. May 03 '18 at 04:51
  • 1
    @Digitalsa1nt Posted the payloads. – Sankarann May 03 '18 at 14:05
  • Thanks, ok so first question. Have you tested sending a message to your app directly through the FCM portal, to rule out a registration problem? – JoeTomks May 03 '18 at 14:11
  • 1
    @Digitalsa1nt if there is any registration problem, I will not receive the notification when the app is in foreground. But am able to receive when it is foreground. So I don't think so, it is a registration issue. – Sankarann May 04 '18 at 00:51
  • Able to get the notification when its run in release mode. What could be the reason? – Sankarann May 04 '18 at 07:05
  • 1
    If it works in release mode, perhaps it's related to the signing of your APK? By default in debug mode, Xamarin.Android doesn't set your app up to be signed. – Architekt May 24 '18 at 23:47
  • 1
    It's all my bad. When you kill the app by stopping the debugger, you will not receive notification. When the relaunch the app and kill it by swipe out from background process, you will receive the notification. – Sankarann Oct 18 '18 at 03:33

0 Answers0