I am trying to send a broadcast when a pause button on a notification is clicked. The notification is fired from a background service that controls a music player.
This is the notification.
private void showPlayerNotification() {
Intent notifyIntent = new Intent(this, PlayerActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(Constants.RADIO_SERVICE_ACTION_RECEIVER);
pauseIntent.putExtra(Constants.RADIO_SERVICE_ACTION, RadioService.PAUSE_ACTION);
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 1, pauseIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentIntent(pendingIntent)
.setStyle(new MediaStyle().setShowActionsInCompactView(0))
.setSmallIcon(R.drawable.play)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setColorized(true)
.setColor(getResources().getColor(R.color.player_background))
.setAutoCancel(true)
.setContentTitle(mediaStream.getStreamName())
.addAction(R.drawable.notification_pause, "Pause", pausePendingIntent);
Notification mediaNotification = notificationBuilder.build();
startForeground(1, mediaNotification);
}
The broadcast receiver is defined in the Service and is registered with an intent filter Constants.RADIO_SERVICE_ACTION_RECEIVER
. I use the same broadcast receiver successfully to control playback where the broadcast originate from an activity.
The above pauseIntent
broadcast is not getting through to the broadcast receiver. Could it be that the service context causing the issue?
I have successfully received the intent in my player activity by using a
PendingIntent.getActivity
but I don't want to handle it in the activity as that might mess the activity code.