Android 自定义通知按钮 onClick 不工作

Android custom notification button onClick not working

这是我的 buildNotification 函数

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

我还有一个 AsyncTask 每 5 秒更新一次远程视图内容..

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

问题是按钮点击在设备上不起作用 运行Oreo 和更高版本?? 它在旧设备上运行良好..

在运行时注册了广播接收器,现在它工作正常:-)

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