Notification.addAction在Android O中不起作用 [英] Notification.addAction not working in Android O

查看:85
本文介绍了Notification.addAction在Android O中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下提到的代码适用于android O verison以下的所有设备.对于android O,addAction()方法不起作用,即按钮单击在android O中不起作用.
任何帮助将不胜感激.

Below mentioned code is working for all devices below android O verison. For android O, addAction() method is not working i.e. button click is not working in android O.
Any help would be appreciated.

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
            mediaPlayerReceiver.putExtra("NewsArticle", news);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaPlayerReceiver, PendingIntent.FLAG_UPDATE_CURRENT);

            Intent dismissNotification = new Intent("com.consult.news.receiver.DISMISS");
            dismissNotification.putExtra("Notification_ID", 1);
            PendingIntent dismissNotificationIntent = PendingIntent.getBroadcast(context, 0, dismissNotification, PendingIntent.FLAG_UPDATE_CURRENT);

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                String CHANNEL_ID = "my_channel_01";
                String CHANNEL_NAME = "my Channel Name";

                NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.setShowBadge(true);
                notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                notificationManager.createNotificationChannel(notificationChannel);
            }

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01")
                    .setSmallIcon(R.drawable.ic_notification_white)
                    .setColor(ContextCompat.getColor(context, R.color.accent))
                    .setContentTitle(context.getString(R.string.Consult_Univadis_Title))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(news))
                    .addAction(isPlaying ? R.drawable.ic_notification_white : R.drawable.ic_notification_white, isPlaying ? "Play" : "Pause", pendingIntent)
                    .addAction(R.drawable.ic_notification_white, "Close", dismissNotificationIntent)
                    .setOngoing(true)
                    .setAutoCancel(false);

            notificationManager.notify(1, builder.build());

推荐答案

我遇到了同样的情况,在Android Oreo中,您需要将其设为明确的Intent(将接收者放在清单上是远远不够的,实际上,不会对此加以注意),因此,当您提出意图时,请使用setClass方法使其明确:

I ran into the same, in Android Oreo you need to make it a explicit Intent (is not enough with putting the receiver on the manifest, in fact, it won't pay attention to that), so when you make the intent, make it explicit using the setClass method:

Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
mediaPlayerReceiver.setClass(this, YourReceiver.class);

其中"this"是上下文,而YourReceiver是您希望监听操作的Receiver类.

Where "this" is the Context and YourReceiver, is the Receiver class that you are expecting to listen to the action.

您必须对 dismissNotification 意图

让我知道这是否对您有用.

Let me know if this worked for you.

这篇关于Notification.addAction在Android O中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆