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

在表示此帖子重复之前,请查看完整的说明。我到处都是,似乎找不到解决我问题的方法。

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

到目前为止,我尝试了什么? 我看过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) {
        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);

有人有什么想法吗?

  

编辑:事实证明,在模拟器上运行时,代码可以正常工作。如here所示,电池优化设置可能有问题。

tgbbhu 回答:通知未显示在Android 8或更高版本中

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

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

编辑   我已经安装了一个测试应用程序,可以在运行Android 9的模拟器上进行尝试,并且通知显示得很好。我的助手类是这样的

public class NotificationHelper {

    public Context ctx;
    public NotificationManager notifyManager;

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

    public void createChannels(String channelId,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())
            }
        });

enter image description 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();
}
本文链接:https://www.f2er.com/3079821.html

大家都在问