Xamarin forms android Notification not Showing when app is running

Nishanthi Kumari 96 Reputation points
2021-04-09T09:55:46.82+00:00

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;
            }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,273 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 66,561 Reputation points Microsoft Vendor
    2021-04-09T11:09:40.217+00:00

    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://learn.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.

    0 comments No comments