Ad

Android Custom Notification Button OnClick Not Working

This is my buildNotification function

  notificationView = new RemoteViews(getPackageName(), R.layout.notification_layout);

        //this is the intent that is supposed to be called when the button is clicked
        ///////////////TOGGLE/////////////////
        Intent toggleIntent = new Intent("com.hbs.andMovie.notification.TOGGLE");
        PendingIntent pendingToggleIntent = PendingIntent.getBroadcast(this, 0, toggleIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_play, pendingToggleIntent);
        ///////////////PLAY NEXT//////////////
        Intent ffIntent = new Intent("com.hbs.andMovie.notification.FF");
        PendingIntent pendingFfIntent = PendingIntent.getBroadcast(this, 0, ffIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_ff, pendingFfIntent);
        ///////////////PLAY PREV//////////////
        Intent rewIntent = new Intent("com.hbs.andMovie.notification.REW");
        PendingIntent pendingRewIntent = PendingIntent.getBroadcast(this, 0, rewIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_rew, pendingRewIntent);
        ///////////////CLOSE//////////////////
        Intent closeIntent = new Intent("com.hbs.andMovie.notification.CLOSE");
        PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0);
        notificationView.setOnClickPendingIntent(R.id.noti_close, pendingCloseIntent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            String NOTIFICATION_CHANNEL_ID = "com.hbs.andmovie";
            String channelName = "DX_Player_Service";
            NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
            chan.setLightColor(Color.BLUE);
            chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(chan);

            notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setOngoing(true)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContent(notificationView)
                    .setWhen(System.currentTimeMillis())
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .build();
        } else {
            notification = new Notification.Builder(AMPlayerCore.this)
                    .setContent(notificationView)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(Notification.PRIORITY_HIGH)
                    .build();
        }

        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
        notification.visibility = Notification.VISIBILITY_PUBLIC;

  //the intent that is started when the notification is clicked
        Intent notificationIntent = new Intent(this, AMPlayerUI.class);
        notificationIntent.setData(uri);
        notificationIntent.putExtra(getString(R.string._mode), AMPlayerUI.mode);
        notification.contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        startForeground(1, notification);

Also I have an AsyncTask to update the remoteview contents in every 5 seconds..

 notificationView.setTextViewText(R.id.noti_title, title);
                    notificationView.setImageViewResource(R.id.noti_thumb, R.drawable.film_strip);
   notificationManager.notify(1, notification);

The problem is button clicks not working on devices running Oreo and higher?? It works fine on older devices..

Ad

Answer

Registered the broadcast receiver at runtime and now it works fine :-)

  private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            assert action != null;
            switch (action) {
                case Intent.ACTION_HEADSET_PLUG:
                    refreshDSP();
                    sendCoreBroadcast(getString(R.string._coreEventReloadDSP_UI));
                    break;
                case "android.media.AUDIO_BECOMING_NOISY":
                    if (prefs.getBoolean(context.getResources().getString(R.string._prefs_key_headphoneCanPause), true)) {
                        pause();
                        AMPlayerCore.isPaused = true;
                    }
                    break;

                case "com.hbs.andMovie.notification.TOGGLE":
                    toggle();
                    break;
                case "com.hbs.andMovie.notification.FF":
                    if (!playNext()) {
                        sendCoreBroadcast(getString(R.string._broadcastActionFailedToChangeMedia));
                    }
                    break;
                case "com.hbs.andMovie.notification.REW":
                    if (!playPrevious()) {
                        sendCoreBroadcast(getString(R.string._broadcastActionFailedToChangeMedia));
                    }
                    break;
                case "com.hbs.andMovie.notification.CLOSE":
                    close_notification();
                    break;

                default:
                    // update(context);
                    break;
            }

        }


    };

 IntentFilter ifilter = new IntentFilter();
        ifilter.addAction("com.hbs.andMovie.notification.TOGGLE");
        ifilter.addAction("com.hbs.andMovie.notification.CLOSE");
        ifilter.addAction("com.hbs.andMovie.notification.FF");
        ifilter.addAction("com.hbs.andMovie.notification.REW");
        ifilter.addAction("android.media.AUDIO_BECOMING_NOISY");
        ifilter.addAction(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(mReceiver, ifilter);
Ad
source: stackoverflow.com
Ad