Xamarin Android Notification not showing

vmorinvmorin Member ✭✭

Hi everyone!

When the notification is recieved following code is handling the message:

private void SendNotification(string message)
{
    var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

    var notificationBuilder = new Notification.Builder(this)
            .SetContentTitle("GCM Message")
            .SetContentText(message)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

     var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
     notificationManager.Notify(0, notificationBuilder.Build());
}

But nothing shows.
I'm debugging it it that would make any difference?
When I go to settings on the app on the device, "Show notifications" is checked.

Thanks for the help!

Best Answer

  • vmorinvmorin ✭✭
    edited July 2018 Accepted Answer

    Reading this helped me:
    https://docs.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough

    Think the main issue was that I didn't had an icon, as soon as I added that the notifcation showed up!
    Result:

    [Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
    public class GcmNotificationService : GcmListenerService
    {
        //More information on how to set different things from notifcation can be found here
        //https://docs.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications
    
        public override void OnMessageReceived(string from, Bundle data)
        {
            var message = data.GetString("message");
            if (!string.IsNullOrEmpty(message))
            {
                if (!NotificationContextHelper.Handle(message))
                    SendNotification(message);
            }
        }
    
        private void SendNotification(string message)
        {
            var builder = new Notification.Builder(this)
                    .SetContentTitle("Title")
                    .SetContentText(message)
                    .SetSmallIcon(Resource.Drawable.notification_test)
                    .SetVisibility(NotificationVisibility.Public);
    
            //If using same Id, the notifcation will be override
            //Use random new for each or increment if you want to show more than one notifcation for this app
            var notificationId = 0;
    
            var notification = builder.Build();
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.Notify(notificationId, notification);
        }
    }
    

Answers

  • vmorinvmorin Member ✭✭

    Got following example from another forum. It says its need a notification channel, but still same issue.
    No errors occur, but nothing show up on the device either.

    using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
    {
        var title = "Title";
        var channelName = "TestChannel"
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            NotificationChannel channel = null;
            if (channel == null)
            {
                channel = new NotificationChannel(channelName, channelName, NotificationImportance.Low)
                {
                    LockscreenVisibility = NotificationVisibility.Public
                };
                channel.SetShowBadge(true);
                notificationManager.CreateNotificationChannel(channel);
            }
            channel.Dispose();
        }
        var bitMap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.notification_template_icon_bg);
        var notificationBuilder = new NotificationCompat.Builder(ApplicationContext)
                                                                .SetContentTitle(title)
                                                                .SetContentText(message)
                                                                .SetLargeIcon(bitMap)
                                                                .SetShowWhen(false)
                                                                .SetChannelId(channelName);
    
        var notification = notificationBuilder.Build();
        notificationManager.Notify(0, notification);
    }
    
  • vmorinvmorin Member ✭✭
    edited July 2018 Accepted Answer

    Reading this helped me:
    https://docs.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough

    Think the main issue was that I didn't had an icon, as soon as I added that the notifcation showed up!
    Result:

    [Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
    public class GcmNotificationService : GcmListenerService
    {
        //More information on how to set different things from notifcation can be found here
        //https://docs.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications
    
        public override void OnMessageReceived(string from, Bundle data)
        {
            var message = data.GetString("message");
            if (!string.IsNullOrEmpty(message))
            {
                if (!NotificationContextHelper.Handle(message))
                    SendNotification(message);
            }
        }
    
        private void SendNotification(string message)
        {
            var builder = new Notification.Builder(this)
                    .SetContentTitle("Title")
                    .SetContentText(message)
                    .SetSmallIcon(Resource.Drawable.notification_test)
                    .SetVisibility(NotificationVisibility.Public);
    
            //If using same Id, the notifcation will be override
            //Use random new for each or increment if you want to show more than one notifcation for this app
            var notificationId = 0;
    
            var notification = builder.Build();
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.Notify(notificationId, notification);
        }
    }
    
  • JoshuaTantonJoshuaTanton USMember ✭✭

    I had the exact implementation as the accepted answer for a long time with no problems. Now about a few months ago It stopped producing a notification. No matter how I do it the xamarin android app will not produce a notification. The code executes with no issue but no notification is created.

  • alexmaniealexmanie ESMember ✭✭

    Same here :neutral:
    Nothing shown on the Android notification center.

    Any new idea on what can be the reason?

  • SuhailAkhtarSuhailAkhtar USMember
    edited July 23

    I have tested following Solution, it worked

    1. You need to create a Notification Channel, and call it OnCreate Method

    `
    public class MainActivity : AppCompatActivity
    {
    private string _channelID = "1001";

    protected override void OnCreate(Bundle bundle)
        {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.Main);
    
            CreateNotificationChannel();
        Notification("Title","Notificaiton Message","Ticker",101);
        }
    
    private void CreateNotificationChannel()
     {
                if (Build.VERSION.SdkInt < BuildVersionCodes.O)
                    return;
    
            var channelName = Resources.GetString(Resource.String.channel_name);
            var channelDescription = GetString(Resource.String.channel_description);
    
            var channel = new NotificationChannel(_channelID, channelName, NotificationImportance.Default)
        channel.Description = channelDescription
    
            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.CreateNotificationChannel(channel);
    }
    
    
    //Notification Builder
    
         private void Notification(string title, string msg, string ticker, int notifId)
         {
    
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,_channelID)
                        .SetContentTitle(title)
                        .SetContentText(msg)
                        .SetTicker(ticker)
                        .SetDefaults((int)NotificationDefaults.Sound)
                        .SetVisibility((int)NotificationVisibility.Public)
                        .SetSmallIcon(Resource.Mipmap.app_icon);
    
                    NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
                    notificationManager.Notify(notifId, builder.Build());
          }
    

    }
    `

  • sharmila13sharmila13 Member ✭✭

    How to dismiss the notification setAutoCancel(true) didnt work out for me. I am testing in Android 9.

Sign In or Register to comment.