Skip to content Skip to sidebar Skip to footer

Xamarin Forms: Push Notification Is Not Working On Android 7.1.2

Push notification is not working on Android device having a version number 7.1.2, but working fine on version 9. Following is my code for showing the notification. if (Build.VERSIO

Solution 1:

I following this sample https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm

I post my SendNotification method from my production project.

Following my working code and change your method.

voidSendNotification (string messageBody, string title)
    {
        var intent = new Intent (this, typeof (SplashActivity));
        intent.AddFlags (ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);


        //if i want more than one notification ,different unique value in every call
        Random u = new Random ();

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O) {

            string channelName = Resources.GetString (Resource.String.channel_name);

            NotificationCompat.Builder notificationBuilder;



                 notificationBuilder = new NotificationCompat.Builder (this, channelName)
                        .SetContentTitle (title)
                        .SetSmallIcon (Resource.Drawable.ic_stat_g)
                        .SetContentText (messageBody)
                        .SetAutoCancel (true)
                        .SetContentIntent (pendingIntent);


            var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;


            NotificationChannel channel = new NotificationChannel (channelName, "notification channel", NotificationImportance.Default);
            notificationManager.CreateNotificationChannel (channel);

            notificationManager.Notify (u.Next(), notificationBuilder.Build ());


        } 
        else
        {

            NotificationCompat.Builder notificationBuilder;


                 notificationBuilder = new NotificationCompat.Builder (this)
                    .SetContentTitle (title)
                    .SetSmallIcon (Resource.Drawable.ic_stat_g)
                    .SetContentText (messageBody)
                    .SetAutoCancel (true)
                    .SetContentIntent (pendingIntent);


            var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;

            notificationManager.Notify (u.Next(), notificationBuilder.Build ());
        }
    }

First of all don't use Android.App.Notification.Builder is deprecated. The NotificationCompat.Builder is the new one.

Maybe this one is your new code

var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            var notificationBuilder = new NotificationCompat.Builder(this)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent);

            var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;

            notificationManager.Notify(0, notificationBuilder.Build());
        }
        else
        {
            var notificationBuilder = new NotificationCompat.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent);


            var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;

            NotificationChannel channel = new NotificationChannel (Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.Default);
            notificationManager.CreateNotificationChannel (channel);

            notificationManager.Notify (0, notificationBuilder.Build ());
        }

Solution 2:

Firebase notifications behave differently depending on the foreground/background state of the receiving app.

Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

Messages with both notification and data payload, when received in the background. In this case, 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.

For more information, visit https://firebase.google.com/docs/cloud-messaging/android/receive

I also have this sample code for you to try:

var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
    NotificationChannel notificationChannel = new NotificationChannel("my_channel", "This is my Notification Channel", NotificationImportance.High);
    builder.SetChannelId("my_channel");
    notificationManager.CreateNotificationChannel(notificationChannel);
}



var notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Immutable))
                          .SetSmallIcon(Resource.Drawable.icon)
                          .SetContentTitle(Header)
                          .SetContentText(body)


        //Set vibrate
        .SetVibrate(newlong[] { 200, 200, 200, 200 })

        //LED
        .SetLights(Android.Graphics.Color.Blue, 1000, 1000)

        //Auto cancel will remove the notification once the user touches it
        .SetAutoCancel(true).Build();


notificationManager.Notify(0, notification);

Post a Comment for "Xamarin Forms: Push Notification Is Not Working On Android 7.1.2"