question

NishanthiKumari-8451 avatar image
0 Votes"
NishanthiKumari-8451 asked LeonLu-MSFT answered

Xamarin forms android Notification not Showing when app is running

I am using azure push notification with xamarin forms. messges are sending successfully, but when app is running its not visible in device.

this is my android code

    public override void OnMessageReceived(RemoteMessage message)
         {
             var notification = message.GetNotification();
             var data = message.Data;
             string body = "";
             string title = "";
             if (data != null && data.ContainsKey("body") && data.ContainsKey("title"))
             {
                 body = data["body"];
                 title = data["title"];
             }
             else if (notification != null)
             {
                 body = message.GetNotification().Body;
                 title = message.GetNotification().Title;
             }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

After you get the content from the RemoteMessage , you need to use code to push Notification.


public override void OnMessageReceived(RemoteMessage message)
    {
        Log.Debug(Tag, "From: " + message.From);
        Log.Debug(Tag, "Notification Message Body: " + message.GetNotification().Body);

        var name = string.Empty;
        if (message.Data.Count > 0)
        {
            Log.Debug(Tag, "Message data payload: " + message.Data);
            name = message.Data["name"];
        }

        var clickAction = message.GetNotification().ClickAction;

        if (!string.IsNullOrEmpty(name))
            SendNotification(message.GetNotification().Body, name, clickAction);
        else
            SendNotification(message.GetNotification().Body);
    }


void SendNotification(string messageBody, IDictionary<string, string> data)
{
    var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    foreach (var key in data.Keys)
    {
        intent.PutExtra(key, data[key]);
    }

    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());
}


For android 8.1 or later, before you push notification, you need to create CreateNotificationChannel as well.

void CreateNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channel = new NotificationChannel(CHANNEL_ID,
                                          "FCM Notifications",
                                          NotificationImportance.Default)
                  {

                      Description = "Firebase Cloud Messages appear in this channel"
                  };

    var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
    notificationManager.CreateNotificationChannel(channel);
}


Here is article about it.

https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.