Push notifications are not displayed on my Android Samsung tablet

Kim Strasser 1,321 Reputation points
2023-02-14T10:31:33.95+00:00

I send push notifications from Azure PlayFab and the notifications are received in foreground mode but they are not displayed. I don't get an error message.

When my application is in foreground mode, then the code in void SendNotification is executed but the push notification is not displayed on my device.

When my application is in background mode, then the code in void SendNotification is not executed when I send a push notification from Azure PlayFab and the notification is not displayed on my device.

I use target Android version 33, minimum Android version 31 and .NET 7.0.

How can I display the push notifications in foreground and background mode on my device?

Activity1.cs:

protected override void OnCreate(Bundle bundle)
{
    CreateNotificationChannel();
}

void CreateNotificationChannel()
{
            var channel = new NotificationChannel(CHANNEL_ID,
                                                  "FCM Notifications",
                                                  NotificationImportance.High)
            {

                Description = "Firebase Cloud Messages appear in this channel"
            };
            channel.EnableVibration(true);
            channel.EnableLights(true);
            channel.LightColor = Android.Graphics.Color.Red;
            channel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification), new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build());
            channel.LockscreenVisibility = NotificationVisibility.Public;

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


MyFirebaseMessagingService.cs:

using System;
using Android.App;
using Android.Content;
using Android.Util;
using Firebase.Messaging;
using System.Collections.Generic;
using AndroidX.Preference;
using Android.Media;
using AndroidX.Core.App;

namespace MyAndroidProject
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";

        public override void OnNewToken(string token)
        {
            Log.Debug(TAG, "Refreshed token: " + token);
            storeToken(token);
        }

        private void storeToken(String token)
        {
            //saving the token on shared preferences
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutString("my_token", token);
            editor.Apply();
        }

        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.Debug(TAG, "From: " + message.From);
            var body = message.GetNotification().Body;
            var title = message.GetNotification().Title;
            Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
            SendNotification(body, title, message.Data);
        }

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

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

            var notificationBuilder = new NotificationCompat.Builder(this, Activity1.CHANNEL_ID)
                                      .SetSmallIcon(Resource.Mipmap.Icon)
                                      .SetContentTitle(Title)
                                      .SetContentText(messageBody)
                                      .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                                      .SetVibrate(new long[] { 1000, 1000, 0, 0, 0 })
                                      .SetLights(Android.Graphics.Color.Red, 3000, 3000)
                                      .SetPriority((int)NotificationPriority.High)
                                      .SetAutoCancel(true)
                                      .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(Activity1.NOTIFICATION_ID, notificationBuilder.Build());
        }
    }
}
Developer technologies .NET Xamarin
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Graham McKechnie 441 Reputation points
    2023-02-15T21:56:34.5566667+00:00

    Android 13 requires POST_NOTIFICATIONS permission.

    You can quickly check by long pressing your application icon and opening App info and turn on Notifications.

    See https://developer.android.com/develop/ui/views/notifications/notification-permission for further details

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-02-15T08:29:53.6433333+00:00

    Hello,

    I send push notifications from Azure PlayFab and the notifications are received in foreground mode but they are not displayed. I don't get an error message.

    If you can get the message in the OnMessageReceived method.

    Please make sure you have created CreateNotificationChannel before you push the notification.

    So, you can try to move CreateNotificationChannel method, Activity1.NOTIFICATION_ID and Activity1.CHANNEL_ID to the OnCreate method of MainAcitivity.cs.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.