5

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());
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Ragini
  • 765
  • 1
  • 11
  • 29

1 Answers1

15

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

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

You will have to do the same for the dismissNotification intent

Let me know if this worked for you.

Javier Vieira
  • 2,100
  • 20
  • 23
  • Thanks it's working but why notification button icons are not displayed in Oreo. Do you have any idea? – Ragini Oct 31 '17 at 10:08
  • 2
    @Ragini you won't get the icons to display on Nougat or later Is a change in the design with Android Nougat. Icons defined by addAction (Notification.Action action) are not any more displayed by devices. They still are required for older devices and Android Wear devices https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html "You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required" – Javier Vieira Oct 31 '17 at 10:58