XamDev89
XamDev89

Reputation: 203

FCM - Only 'display' push notification when app is in background

Currently, I have push notifications being sent and received in my Xamarin app. When I send a push notification the sneak peak dialogue is displayed when the app is in the foreground & also the background. I am trying to only visual display a push notification when the app is in the background. When the user is in the foreground the notification just sneaks into the HUD and is not displayed.

Here is my code:

Xamarin Android

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMMessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        try
        {
            var _message = message.GetNotification().Body;
            var _title = message.GetNotification().Title;
            MainActivity.SendNotification(_title, _message);
        }
        catch (Exception e)
        {
            //Was NotificationType null? this should never be null anyways
        }
    }
}

API - Android Payload

    return new JObject(
           new JObject(
                   new JProperty("notification",
                       new JObject(
                           new JProperty("body", message),
                           new JProperty("title", title),
                           new JProperty("icon","toasticon"), 
                           new JProperty("user", fromId != null ? fromId.ToString() : "")
                        )
                    ),
                   new JProperty("data",
                        new JObject(
                            new JProperty("notificationtype", notificationType)
                            )
                   )
                )
       ).ToString();

Upvotes: 1

Views: 1618

Answers (1)

FreakyAli
FreakyAli

Reputation: 16572

If you have followed the firebase setup for xamarin android

You might have read there that foreground notifications have to be displayed manually,

How to do that?

This is how

In the setup, you will have a class inheriting from FirebaseMessagingService

In that class, there will be a piece of code something like this():

 var pendingIntent = PendingIntent.GetActivity(this,
                                              MainActivity.NOTIFICATION_ID,
                                              intent,
                                              PendingIntentFlags.OneShot);

var notificationBuilder = new  NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
                          .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                          .SetContentTitle("FCM Message")
                          .SetContentText(messageBody)
                          .SetAutoCancel(true)
                          .SetContentIntent(pendingIntent);

 var notificationManager = NotificationManagerCompat.From(this);
 notificationManager.Notify(MainActivity.NOTIFICATION_ID, 
 notificationBuilder.Build());

This piece of code sends the notification to the Android tray, When in the foreground and hence if you comment this you will get the desired output.

Good luck!

In case of queries revert.

Upvotes: 1

Related Questions