简体   繁体   中英

Notification not showing up in Android 8 or higher

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 .

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())
            }
        });

在此处输入图像描述

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM