Уведомления действий не работает в Android - PullRequest
0 голосов
/ 13 октября 2018

Я работаю над Android-приложением для потоковой передачи.Когда я начинаю потоковую передачу, я устанавливаю уведомление, в котором есть несколько кнопок.Нажатие кнопок работает, если я нахожусь на том же экране, на котором было сгенерировано уведомление, но когда я перехожу к другому виду деятельности или домашнему экрану устройства, кнопки не работают.Я установил ожидающие намерения и зарегистрировал также широковещательный приемник.

Ниже приведен код, который я использую для установки уведомления в MusicPlayerActivity.java:

public void setCustomNotification(final Bitmap image) {
    String ns = Context.NOTIFICATION_SERVICE;
    notificationManager = (NotificationManager) getSystemService(ns);

    Intent deleteIntent = new Intent(this, NotificationReceiver.class);
    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mNotifyBuilder = new Notification.Builder(this);
    notification = mNotifyBuilder.setContentTitle(stationToPlay.getName())
            .setContentText(stationToPlay.getName())
            .setSmallIcon(R.drawable.mic_icon)
            .setLargeIcon(image)
            //.addAction(R.id.play_pause_btn, "", playIntent)
            .build();
    mNotifyBuilder.setPriority(Notification.PRIORITY_LOW);

    //binding notification view with remote view
    notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);
    notificationView.setImageViewResource(R.id.play_pause_btn, R.drawable.pause_circle_icon);
    if (stationToPlay.isFavorite()) {
        notificationView.setImageViewResource(R.id.fav_btn, R.drawable.heart_fill_icon);
    } else {
        notificationView.setImageViewResource(R.id.fav_btn, R.drawable.heart_icon);
    }
    //notificationView.setImageViewBitmap(R.id.img_station, image12);
    notificationView.setCharSequence(R.id.station_name, "setText", stationToPlay.getName());

    //line to go on muscic play activity when click on notification
    Intent notificationIntent = new Intent(this, MusicExoPlayerActivity.class);
    notificationIntent.putExtra(Constants.seekTo, (int) exoPlayer.getCurrentPosition());
    notificationIntent.putExtra(NOTIFICATION_CLICK_ACTION, true);
    Station stationObj = AppUtils.getLastPlayedStation();
    notificationIntent.putExtra(Constants.STATION_TO_PLAY, stationObj);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;

    notification.flags |= Notification.FLAG_NO_CLEAR;

    Intent switchIntent = new Intent(ACTION_PLAY_PAUSE);
    PendingIntent playIntent = PendingIntent.getBroadcast(MusicExoPlayerActivity.this, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent switchIntent1 = new Intent(ACTION_FAV);
    PendingIntent pendingSwitchIntent1 = PendingIntent.getBroadcast(MusicExoPlayerActivity.this, 101, switchIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent switchIntent2 = new Intent(ACTION_STOP);
    PendingIntent pendingSwitchIntent2 = PendingIntent.getBroadcast(MusicExoPlayerActivity.this, 102, switchIntent2, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationView.setOnClickPendingIntent(R.id.play_pause_btn, playIntent);
    notificationView.setOnClickPendingIntent(R.id.fav_btn, pendingSwitchIntent1);
    notificationView.setOnClickPendingIntent(R.id.stop_btn, pendingSwitchIntent2);
    notificationManager.notify(NOTIFICATION_ID, notification);
}

Ниже приведеншироковещательный приемник для него, который также определен в MusicPlayerActivty.java:

BroadcastReceiver notificationReciever = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equalsIgnoreCase(Constants.ACTION_FAV)) {
            MakeToast.show("Fav noti clicked");
            ivFav.performClick();
        } else if (action.equalsIgnoreCase(Constants.ACTION_STOP)) {
            MakeToast.show("Stop noti clicked");
            if (Constants.isMediaPlaying) {
                notificationView.setImageViewResource(R.id.play_pause_btn, R.drawable.play_circle_icon);
                notification.contentView = notificationView;
                notificationManager.notify(1, notification);
                ivStop.performClick();
            }
            notificationManager.cancel(1);

        } else if (action.equalsIgnoreCase(Constants.ACTION_PLAY_PAUSE)) {
            MakeToast.show("play/pause noti clicked");
            if (Constants.isMediaPlaying) {
                notificationView.setImageViewResource(R.id.play_pause_btn, R.drawable.play_circle_icon);
            } else {
                notificationView.setImageViewResource(R.id.play_pause_btn, R.drawable.pause_circle_icon);
            }
            notification.contentView = notificationView;
            notificationManager.notify(1, notification);
            ivPlayPause.performClick();
        }
    }
};

В onResume () я регистрирую все эти получатели.

registerReceiver(notificationReciever, new IntentFilter(ACTION_PLAY_PAUSE));
        registerReceiver(notificationReciever, new IntentFilter(ACTION_STOP));
        registerReceiver(notificationReciever, new IntentFilter(ACTION_FAV));

Теперь, если перейти к операции, скажем ActivityB инажмите на действия в уведомлении, ничего не происходит.Может кто-нибудь сказать мне, что я делаю не так?

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...