MediaPlayer Notification Controls Not Working - android

I have the following code that creates an ongoing notification for my music player
private Notification buildNotification(String title, String artist) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setOngoing(true);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);
Intent previousTrackIntent = new Intent(Music.getContext(), MusicService.class);
previousTrackIntent.setAction(MusicService.ACTION_REWIND);
PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, previousTrackIntent, 0);
Intent playPauseTrackIntent = new Intent(Music.getContext(), MusicService.class);
playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, playPauseTrackIntent, 0);
Intent nextTrackIntent = new Intent(Music.getContext(), MusicService.class);
nextTrackIntent.setAction(MusicService.ACTION_SKIP);
PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, nextTrackIntent, 0);
Intent stopServiceIntent = new Intent(Music.getContext(), MusicService.class);
stopServiceIntent.setAction(MusicService.ACTION_STOP);
PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, stopServiceIntent, 0);
if (mState == State.Playing) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
} else if (mState == State.Paused || mState == State.Stopped) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
}
compactNotification.setTextViewText(R.id.notification_base_line_one, title);
compactNotification.setTextViewText(R.id.notification_base_line_two, artist);
expandedNotification.setTextViewText(R.id.notification_base_line_one, title);
expandedNotification.setTextViewText(R.id.notification_base_line_two, artist);
compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
//Set the album art.
expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
compactNotification.setImageViewBitmap(R.id.notification_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
//Attach the shrunken layout to the notification.
notificationBuilder.setContent(compactNotification);
//Build the notification object.
Notification notification = notificationBuilder.build();
//Attach the expanded layout to the notification and set its flags.
if (Build.VERSION.SDK_INT >= 16)
notification.bigContentView = expandedNotification;
notification.flags = Notification.FLAG_FOREGROUND_SERVICE |
Notification.FLAG_NO_CLEAR |
Notification.FLAG_ONGOING_EVENT;
return notification;
}
The notification appears correctly but the controls dont work even thought the controls are well defined.
Is there something else I need to enable or assign to get the controls to work?

I know this answer is 5 years late, but I found that Notification.FLAG_FOREGROUND_SERVICE still shows the controls, but doesn't allow them to be "clickable". I found the same behavior when I showed the notification with startForeground(int,notification);
If I just used the following flags (Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT), then call stopForeground(false); and then nm.notify(int, notification); it works as expected.

Related

Notification coloured action button Android 10

I'm trying to color the buttons(Action) in Notification like this.
So far this is what i'm achieved so far.
Below is the code i'm using
NotificationService.class
private void showCallNotification(Map<String, String> dataMap) {
notificationId = (int) (System.currentTimeMillis() % 10000);
Intent intent = new Intent(getApplicationContext(), ReceiveCallActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setAction(Intent.ACTION_ANSWER)
.putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_ACCEPTED)
.putExtra("title", dataMap.get("title"))
.putExtra("action", dataMap.get("action"));
Intent cancelIntent = new Intent(getApplicationContext(), VideoCallReceiver.class)
.setAction(AppConstants.INCOMING_CALL_BROADCAST_ACTION)
.putExtra(AppConstants.CALL_STATUS, AppConstants.CALL_DECLINED)
.putExtra("notificationId", notificationId);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action declineAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.decline_call), cancelPendingIntent);
NotificationCompat.Action acceptAction = new NotificationCompat.Action(android.R.drawable.ic_menu_call, getString(R.string.accept_call), pendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(dataMap.get("sender"))
.setContentText(getString(R.string.incoming_call))
.setColor(getResources().getColor(R.color.notification_color))
.setAutoCancel(true)
.setTimeoutAfter(CALL_DISMISS_TIME)
.addAction(declineAction)
.addAction(acceptAction)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setFullScreenIntent(pendingIntent, true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createCallNotificationChannel();
}
notificationManager.notify(notificationId, builder.build());
}
I'm out of ideas now. Any help will be appreciated.
Code taken from the Android official Dialer app
when you set the action string getString(R.string.decline_call) change that with a call to the method
private Spannable getActionText(#StringRes int stringRes, #ColorRes int colorRes) {
Spannable spannable = new SpannableString(context.getText(stringRes));
if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
spannable.setSpan(
new ForegroundColorSpan(context.getColor(colorRes)), 0, spannable.length(), 0);
}
return spannable;
}
I have managed to apply colors for the notification action buttons. Use spannable just like Aeron suggested (code from the Dialer app) and the very important thing is to set USE_FULL_SCREEN_INTENT permission. Otherwise colors are ignored in the dark mode or look incorrect in the non dark mode.
You can use custom layout in your notification. There are setCustomContentView() and setCustomBigContentView methods:
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNE L_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
Do have a read here
Use custom drawable like this
NotificationCompat.Action declineAction = new NotificationCompat.Action(R.drawable.ic_red, getString(R.string.decline_call), cancelPendingIntent);
NotificationCompat.Action acceptAction = new NotificationCompat.Action(R.drawable.ic_green, getString(R.string.accept_call), pendingIntent);

Open application after clicking on notification Android

I want to run my application after notification click!
I want to run: app-->com.zaglebiefm.Radio
This file diectory: library-->com.zaglebiefm.library.Notyfication
Notification.class:
private void buildNotification() {
Intent intentPlayPause = new Intent(NOTIFICATION_INTENT_PLAY_PAUSE);
Intent intentOpenPlayer = new Intent(NOTIFICATION_INTENT_OPEN_PLAYER);
Intent intentCancel = new Intent(NOTIFICATION_INTENT_CANCEL);
PendingIntent playPausePending = PendingIntent.getBroadcast(this, 23, intentPlayPause, 0);
PendingIntent openPending = PendingIntent.getBroadcast(this, 31, intentOpenPlayer, 0);
PendingIntent cancelPending = PendingIntent.getBroadcast(this, 12, intentCancel, 0);
RemoteViews mNotificationTemplate = new RemoteViews(this.getPackageName(), R.layout.notification);
Notification.Builder notificationBuilder = new Notification.Builder(this);
if (artImage == null)
artImage = BitmapFactory.decodeResource(getResources(), R.drawable.default_art);
mNotificationTemplate.setTextViewText(R.id.notification_line_one, singerName);
mNotificationTemplate.setTextViewText(R.id.notification_line_two, songName);
mNotificationTemplate.setImageViewResource(R.id.notification_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mNotificationTemplate.setImageViewBitmap(R.id.notification_image, artImage);
mNotificationTemplate.setOnClickPendingIntent(R.id.notification_play, playPausePending);
Notification notification = notificationBuilder
.setSmallIcon(smallImage)
.setContentIntent(openPending)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContent(mNotificationTemplate)
.setUsesChronometer(true)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
RemoteViews mExpandedView = new RemoteViews(this.getPackageName(), R.layout.notification_expanded);
mExpandedView.setTextViewText(R.id.notification_line_one, singerName);
mExpandedView.setTextViewText(R.id.notification_line_two, songName);
mExpandedView.setImageViewResource(R.id.notification_expanded_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mExpandedView.setImageViewBitmap(R.id.notification_image, artImage);
mExpandedView.setOnClickPendingIntent(R.id.notification_expanded_play, playPausePending);
notification.bigContentView = mExpandedView;
}
if (mNotificationManager != null)
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
I tried some things but they won't work for me.
Any ideas or solutions.
Use the below code instead to launch desired/targetted application activity.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,YourTargetActivity.class), 0);
mBuilder.setContentIntent(pendingIntent);
This should work.
Intent myintent = new Intent(this, Splash_Activity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
int randomPIN = (int)(Math.random()*9000)+1000;
PendingIntent conIntent = PendingIntent.getActivity(this, randomPIN,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(conIntent);
This will work

Notification with multiple buttons

I need to display notification with two buttons. Different operation need to perform for each button.so for that I have written following code but when I'm getting multiple notification delete action is not performing.
Random NOTIFICATION_ID = new Random();
int CANCELNOTIFICATIONID = NOTIFICATION_ID.nextInt();
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Log.i("******* Service6", "" + msg);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(GcmIntentService.this, LoginActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(GcmIntentService.this, 0, intent, 0);
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
deleteIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Delete loops");
Trace.i(TAG, "Looptype Delete loop");
deleteIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
deleteIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
deleteIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pDeleteIntent = PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent archiveIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
Trace.i(TAG, "Looptype Archive loop");
archiveIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Archive loops");
archiveIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
archiveIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
archiveIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pArchiveIntent = PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle("Sample");
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mPushtext));
notificationBuilder.setContentText(mPushtext);
notificationBuilder.setSound(soundUri);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notificationBuilder.setAutoCancel(false);
// notificationBuilder.setContentIntent(pArchiveIntent);
Log.i("GCMIntent Srevice5", "" + msg);
notificationBuilder.setOngoing(true);
mNotificationManager.notify(CANCELNOTIFICATIONID, notificationBuilder.build());
How to solve this issue.
You need to use different requestCode in Pending intent
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 1, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 2, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
In the below way i have solved it.
in the intent i have given two differentactivities
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteLoopActivity.class);
Intent archiveIntent = new Intent(GcmIntentService.this, ArchiveLoopActivity.class);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, deleteIntent, 0));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, archiveIntent, 0));
The above code solves my problem
Thanks all

Change button background on click event , notification bar

i want to Change button background on click event , notification bar
for eg. i have button with text play which is on notification bar when i click on play it should be change to pause and again if i click on pause this pause text should be changed to play text, how can we do it any help is appreciated
thanks in advance
it is more like music player has on going notification so we can play and pause the media using notification bar as well.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentv = new RemoteViews(getPackageName(),
R.layout.notification_bar);
int icon = R.drawable.noti_launcher;
CharSequence tickerText = "notification";
long when = System.currentTimeMillis(); // now
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
notification.contentView = contentv;
Intent laucherIntent = new Intent(this, NavigationActivity.class);
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
PendingIntent launcherPI = PendingIntent.getActivity(this, 0,
laucherIntent, 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Intent flaotIntent = new Intent(this, FloatMainActivity.class);
PendingIntent floatPI = PendingIntent.getActivity(this, 0, flaotIntent,
0);
PendingIntent wifiPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_WIFI), 0);
PendingIntent btPI = PendingIntent.getBroadcast(context, 0, new Intent(
Constants.ACTION_BLUETOOTH), 0);
PendingIntent mobileDataPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_DATA), 0);
PendingIntent alarmsPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.CUSTOM_ACTION), 0);
contentv.setImageViewResource(R.id.bluetooth, R.drawable.noti_wifi);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentv.setOnClickPendingIntent(R.id.bluetooth, btPI);
contentv.setOnClickPendingIntent(R.id.wifi, wifiPI);
contentv.setOnClickPendingIntent(R.id.data, mobileDataPI);
contentv.setOnClickPendingIntent(R.id.alarm, pi);
contentv.setOnClickPendingIntent(R.id.icon, launcherPI);
contentv.setOnClickPendingIntent(R.id.float_notification, floatPI);
notificationManager.notify(1, notification);
contentv.setInt(R.id.viewId, "change", R.drawable.nextId);
you can use this for change background image.

Why my notification doesn't appear

Hello guys Im using advance notification manager this is the code
Notification note = new Notification();
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notif);
contentView.setImageViewResource(R.id.image, R.drawable.notif);
contentView.setTextViewText(R.id.title, "Focused Crawling");
contentView.setTextViewText(R.id.text, "Crawling In Progress....");
note.contentView = contentView;
Intent notificationIntent = new Intent(this, LoadingActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
note.contentIntent = contentIntent;
note.vibrate = new long[] { 500L, 200L, 200L, 500L };
note.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "2");
note.flags |= Notification.FLAG_NO_CLEAR;
mgr.notify(1337, note);
startForeground(1337, note);
and why it didnt appear at the status bar ?
You don't specify an icon for the status bar, which is required.
Add a line like this to your code:
note.icon = R.drawable.youricon;
See Creating a Notification

Categories

Resources