0

Before indicating this post as a duplicate, please look at the complete description. I have been looking everywhere and cannot seem to find the solution to my problem.

The problem In my application, notifications don't show up on Android 8 or higher. I cannot seem to get any notification to show up at all.

What did I try so far? I looked at this post, this post, a couple of tutorials like this one.
All talk about setting the smallIcon, contentTitle, contentText and a notification channel. I did all of this, but without any apparent success.

My code

In my NotificationHelper class I have the following methods:

    public NotificationHelper(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android 8 or higher? Set notification channel
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
        }
    }

    private NotificationManager getManager() {
        if (notifyManager == null) {
            notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notifyManager;
    }

    public Notification getNotification1(Context context) {
        NotificationCompat.Builder  builder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
                .setContentTitle("New Message")
                .setContentText("You've received new messages.")
                .setSmallIcon(R.drawable.ic_notification_alert); // alert icon.

        return builder.build();
    }

    public void notify(int id, Notification notification) {
        Log.i("NotificationHelper", "Notifying with notification: " + notification.toString());
        getManager().notify(id, notification);
    }

And then in my MainActivity I have the following method in an onButtonClick event (obviously after instantiating the notificationHelper object in the MainActivity's onCreate() method):

Notification notification = notificationHelper.getNotification1(this);
notificationHelper.notify(NotificationHelper.notification_one, notification);

Does anyone have any ideas?

EDIT: It turns out the code works fine when run on an emulator. It could be a problem with the battery optimization settings, as specified here.

Jorn Rigter
  • 745
  • 1
  • 6
  • 25
  • What does `NotificationHelper` extend? Why are you passing a `Context` in the `super` constructor call, and passing one into the `getNotification1()` method? Why are you using `CHANNEL_ONE_ID` to create the `NotificationChannel`, but the local `CHANNEL_ID` to create the `Notification`? Do those have the same value? If so, why aren't you just using the field everywhere? Are you getting any relevant errors or messages in your logcat? – Mike M. Nov 18 '19 at 15:33
  • It's extending ContextWrapper, that's also why the Context is given in the constructor of NotificationHelper. The channel id is a good point, I changed it but still giving me no result. I am not getting any relevant error messages no... – Jorn Rigter Nov 18 '19 at 15:43
  • How do you create this notifications? Is it created inside your app or by external API (e.g. Firebase Cloud Messages)? – MarkWalczak Nov 20 '19 at 06:17

2 Answers2

1

I suspect this issue has to do with how the channel is being created. Change create channel to below

public void createChannels(String channelId, String channelName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android 8 or higher? Set notification channel
            NotificationChannel notificationChannel = new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
        }
    }

then remove createChannels in your constructor and place it in getNotification1

public Notification getNotification1(Context context) {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.

        createChannels(CHANNEL_ID,CHANNEL_NAME)

        NotificationCompat.Builder  builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle("New Message")
                .setContentText("You've received new messages.")
                .setSmallIcon(R.drawable.ic_notification_alert); // alert icon.

        return builder.build();
    }

EDIT I have setup a test app to try it out on an emulator running Android 9 and notification is showing just fine. My helper class looks like this

public class NotificationHelper {

    public Context ctx;
    public NotificationManager notifyManager;

    public NotificationHelper(Context base) {
        ctx  = base;
    }

    public void createChannels(String channelId, String channelName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android 8 or higher? Set notification channel
            NotificationChannel notificationChannel = new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
        }
    }

    private NotificationManager getManager() {
        if (notifyManager == null) {
            notifyManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notifyManager;
    }

    public void notify(int id, Notification notification) {
        Log.i("NotificationHelper", "Notifying with notification: " + notification.toString());
        getManager().notify(id, notification);
    }

    public Notification getNotification1() {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.

        createChannels(CHANNEL_ID,"name");

        NotificationCompat.Builder  builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
                .setContentTitle("New Message")
                .setContentText("You've received new messages.")
                .setSmallIcon(R.drawable.ic_launcher_foreground); // alert icon.

        return builder.build();
    }
}

and I test it out this way

NotificationHelper helper = NotificationHelper(this);
helloWorldTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                helper.notify(10,helper.getNotification1())
            }
        });

enter image description here

Networks
  • 2,144
  • 1
  • 10
  • 17
0

The way you initialize CHANNEL_ID in getNotification1() seems suspect. I'd write this instead:

public Notification getNotification1(Context context) {

    NotificationCompat.Builder  builder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_notification_alert); // alert icon.

    return builder.build();
}

EDIT: Can you try using a custom layout with RemoteViews

public Notification getNotification1() {
    String CHANNEL_ID = "my_channel_01";// The id of the channel.

    createChannels(CHANNEL_ID,"name");

    NotificationCompat.Builder  builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_launcher_foreground); // alert icon.

    RemoteViews contentView = new RemoteViews(R.layout.notification_custom_layout);
    contentView.setTextViewText(R.id.notification_title, "App name");
    contentView.setTextViewText(R.id.notification_text, "Your msg");
    mBuilder.setContent(contentView);

    return builder.build();
}
matdev
  • 4,115
  • 6
  • 35
  • 56
  • Thanks for the reply. I changed it to both use the CHANNEL_ONE_ID, but with no success still.. – Jorn Rigter Nov 18 '19 at 15:44
  • Have you extended FirebaseMessagingService ? Do you enter onMessageReceived() ? – matdev Nov 18 '19 at 15:48
  • Yes and yes. Only the notification is not showing up.. I am now testing with a simple button to test whether I am able to send a notification at all. – Jorn Rigter Nov 18 '19 at 18:07