0

I try create app with Xamarin Form that can receive notification both foreground and background on Android. For foreground, it works fine. I create firebase service to handle foreground notification. However, I cannot find the way to make background notification work.

First, I try to update manifest to support this event. enter image description here

Secondly, I add some code to my splashactivity to handle background notification. enter image description here

Next, I use the following PHP code to send notification to Firebase server.enter image description here

On my device, I can see the above notification.enter image description here

I tap notification to open my app. However, nothing happened and device log doesn't give me any idea about what going on.

enter image description here

Is there any incorrect in my code?

Thanks,

  • Possible duplicate of [How to handle notification when app in background in Firebase](http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – X3Btel May 08 '17 at 21:06
  • 1
    Please do not use images for code/text: https://meta.stackoverflow.com/questions/285551/why-not-to-upload-images-of-code-on-so-when-asking-a-question – SushiHangover May 09 '17 at 02:29
  • @X3Btel My question is all about Xamarin not native Android. Several things are quite different. –  May 09 '17 at 08:19
  • can you post your full code of SplashActivity - not as picture – X3Btel May 09 '17 at 08:38
  • @X3Btel There are only 2 methods in SplashActivity with exact same code. I think my code should work fine because there is no error log in device log below. –  May 09 '17 at 09:45

1 Answers1

0

Taken from Firebase documentation:

Handle notification messages in a backgrounded app When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

I was able to solve this by sending Custom Data with the FCM payload and then in MainActivity OnCreate() method:

var someValue = Intent.GetStringExtra("YourKey");
// if result not null then handle how you like...

In my case, I wanted to redirect the user to the view the notification is for.

Update:

There is one more way to handle this.

Override OnMessageReceived method...

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    public override void OnMessageReceived(RemoteMessage message)
    {
        SendNotification(message?.Data);
    }

    void SendNotification(IDictionary<string, string> data)
    {
        // handle your data that got passed
    }
}

And instead of doing this in your example:

{
    "noticiation" : {
        "title" : "Test 456",
        "text" : "Otra 123123"
    }
}

Pass data instead:

{
    "data" : {
        "title" : "Test 456",
        "text" : "Otra 123123"
    }
}

OnMessageReceived will now be called both when your app is in the background and in the foreground.

Note that this method will mean you can't use the console in Firebase to send messages because as I'm aware, it includes "notification" in the payload.

Greg R Taylor
  • 3,470
  • 1
  • 25
  • 19