Android 8 或更高版本中未显示通知

Notification not showing up in Android 8 or higher

在将此 post 表示为重复之前,请查看完整说明。我一直在到处寻找,似乎无法找到解决我的问题的方法。

问题 在我的应用程序中,通知不会显示在 Android 8 或更高版本上。我似乎根本无法收到任何通知

到目前为止我尝试了什么? 我看了 post, this post, a couple of tutorials like this 一个。
所有关于设置 smallIcon、contentTitle、contentText 和通知通道的讨论。我做了所有这些,但没有任何明显的成功。

我的代码

在我的 NotificationHelper class 我有以下方法:

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

然后在我的 MainActivity 中,我在 onButtonClick 事件中有以下方法(显然是在 MainActivity 的 onCreate() 方法中实例化 notificationHelper 对象之后):

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

有没有人有什么想法?

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.

您在 getNotification1() 中初始化 CHANNEL_ID 的方式似乎令人怀疑。 我会这样写:

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

编辑: 您可以尝试使用 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();
}

我怀疑这个问题与频道的创建方式有关。将创建频道更改为以下

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

然后在构造函数中删除 createChannels 并将其放入 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();
    }
}

我也是这样测试的

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

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