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

问题描述 投票:1回答:2

在将此帖子重复之前,请先查看完整的说明。我到处都在寻找,似乎无法找到解决问题的方法。

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

到目前为止我尝试了什么?我看过this帖子,this帖子,以及一些类似this的教程。所有人都在谈论设置smallIcon,contentTitle,contentText和通知通道。我做了所有这些,但是没有任何明显的成功。

我的代码

在我的NotificationHelper类中,我具有以下方法:

    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) {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.
        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();
    }

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

有人有什么想法吗?

android android-notifications android-8.0-oreo
2个回答
0
投票

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

0
投票

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

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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.