NPadrutt
NPadrutt

Reputation: 4307

Android local notification not shown

I have a Xamarin Android App and try to send local notifications. I have this method:

    public void SendNotification(Context context, string message)
    {
        var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);


        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            var notificationChannel = new NotificationChannel(CHANNEL_ID, Strings.ApplicationName, NotificationImportance.High);
            notificationManager.CreateNotificationChannel(notificationChannel);
        }

        var intent = new Intent(context, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
        var bigstyle = new Notification.BigTextStyle();
        bigstyle.BigText(message);
        bigstyle.SetBigContentTitle(Strings.ApplicationName);
        var notificationBuilder = new Notification.Builder(context)
            .SetSmallIcon(Resource.Drawable.Icon) //Icon  
            .SetContentTitle(Strings.ApplicationName) //Title  
            .SetContentText(message) //Message  
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent)
            .SetStyle(bigstyle);

        var notification = notificationBuilder.Build();

        notificationManager.Notify(MESSAGE_TYPE_DEFAULT, notification);
    }

For Test purposes I have placed a call in the On Create Method in my MainActivity:

            new NotificationService().SendNotification(this, "Test");

The last time I tested this, was with Android 7.0 and that worked. With Android 8.0 There is no error thrown or something like that. I also can't see any error in the output. But it just doesn't show any notification.

Has there anything changed with 8.0 or am I doing something wrong in general?

Upvotes: 0

Views: 2881

Answers (2)

Rino
Rino

Reputation: 1215

You have to use setChannelId from Android O. Sharing the java code for the same.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = getResources().getString(R.string.channel_id);// The id of the channel.
            CharSequence name = getString(R.string.app_name);// The user-visible name of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationManager.createNotificationChannel(mChannel);
            Notification.Builder b = new Notification.Builder(BaseActivity.this, CHANNEL_ID);
            b.setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(icon)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setChannelId(CHANNEL_ID)
                    .setContentIntent(contentIntent);
            Notification notification = b.build();
            notificationManager.notify(notificationType, notification);
        }

Upvotes: 2

Vatsal Desai
Vatsal Desai

Reputation: 179

You need to add NotificationChannel in notificationManager. You will get detail information in this link. https://www.androidauthority.com/android-8-0-oreo-app-implementing-notification-channels-801097/

Upvotes: 1

Related Questions