Buttons are not displayed in notification - android

I am creating a custom notification in service (MusicService) and adding buttons as follow.
//Notification
Intent notIntent = new Intent(this, MainActivity.class);
notIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Play
Intent playIntent = new Intent(this, MusicService.class);
playIntent.setAction(ACTION_PLAY);
PendingIntent pPlayIntent = PendingIntent.getService(this, 1,
playIntent, 0);
//Pause
Intent pauseIntent = new Intent(this, MusicService.class);
pauseIntent.setAction(ACTION_PAUSE);
PendingIntent pPauseIntent = PendingIntent.getService(this, 1,
playIntent, 0);
//Previous
Intent previousSongIntent = new Intent(this, MusicService.class);
previousSongIntent.setAction(ACTION_PREVIOUS);
PendingIntent pPreviousSongIntent = PendingIntent.getService(this, 1,
previousSongIntent, 0);
//Next
Intent nextSongIntent = new Intent(this, MusicService.class);
nextSongIntent.setAction(ACTION_NEXT);
PendingIntent pNextSongIntent = PendingIntent.getService(this, 1,
nextSongIntent,0);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentText(songTitle)
.setContentIntent(pendInt)
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
.addAction(R.drawable.ic_play_button, "PLAY", pPlayIntent)
.addAction(R.drawable.ic_pause_button, "PAUSE", pPauseIntent)
.addAction(R.drawable.ic_next_button, "NEXT", pNextSongIntent)
.addAction(R.drawable.ic_previous_button, "PREV", pPreviousSongIntent);
Notification notification = builder.build();
startForeground(NOTIFY_ID, notification);
Buttons icons are not displayed in notification.
Using two fingers and swiping down on notification display text (PLAY PAUSE NEXT).
Am I missing something?

Use MediaStyle with setShowActionsInCompactView
builder.setStyle(androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2))
Full example here https://code.videolan.org/videolan/vlc-android/-/blob/master/application/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt#L99

Related

shutting the app by removing notification

#RequiresApi(api = Build.VERSION_CODES.M)
public void showNotification(int playPauseBtn) {
Intent intent = new Intent(this, ExoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
Intent prevIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PREVIOUS);
PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 0, prevIntent,
PendingIntent.FLAG_MUTABLE);
Intent playIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PLAY);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 0, playIntent,
PendingIntent.FLAG_MUTABLE);
Intent nextIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_NEXT);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 0, nextIntent,
PendingIntent.FLAG_MUTABLE);
Bitmap picture = BitmapFactory.decodeResource(getResources(), HelperClass.getlist().get(position).getImg());
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
.setSmallIcon(HelperClass.getlist().get(position).getImg()) //get thumbnail will have small icon pic
.setLargeIcon(picture)
.setContentTitle(HelperClass.getlist().get(position).getName())
.addAction(R.drawable.ic_baseline_skip_previous_24, "Previous", prevPendingIntent)
.addAction(R.drawable.ic_baseline_play_arrow_24, "Play", playPendingIntent)
.addAction(R.drawable.ic_baseline_skip_next_24, "Next", nextPendingIntent)
//.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
//.setMediaSession(mediaSession.getSessionToken()))
.setPriority(Notification.PRIORITY_HIGH)
.setC`your text`ontentIntent(contentIntent)
.setOnlyAlertOnce(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
This code creates Notification wherever this 'showNotification()' function is called but what if I want to shut the app by removing the notification?
use setDeleteIntent method - when notification is removed you can send some broadcast to all your "contextual" ojects (e.g. Activity, Service)
note that there is also setOngoing method, which will stick your notification and user won't be able to remove it "manually". then you can provide "kill" action button for this notification, which may behave exacly same as one created for setDeleteIntent. in that case don't forget to remove notification when user just leave/exit your app, when "kill" action pressed or in any other "usual" way

local Notification on click open an activity

I have a local notification which is set in one of my receivers. Onclick of notification I want to open a specific activity. Below is my code.
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(times, builder.build());
But this is not working. On Click of notification it just removes notification from status bar. Am I missing anything ?
try this :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
instead of this :
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
use this codes before your notification builder code.
now this is the full version :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(times, notificationBuilder.build());
try to use this. i hope you will get you desire answer.
UPDATE
here is the sample project : https://github.com/ImtiazDipto01/SimpleNotification
Add Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP as Flag on your `Intent. Like -
notificationIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Also provide unique id for notification. like -
int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNotificationId , notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Also on -
manager.notify(uniqueNotificationId , builder.build());

How to set ongoing flags in builder notification?

How do I add flags in my notification created using the Notification.Builder builder? It was straightforward to add flags to the Notification noti = new NotificationCompat.Builder(this) notification declaration but not to the one I've referenced above. Here is what I've tried so far:
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Download [ROOT]")
//.setContentText("This is a test notification")
.setAutoCancel(false).setOngoing(true);
Intent notificationIntent = new Intent(this, NotificationAction.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(111, builder.build());
for setOngoing() to Notification.Builder try this :
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentIntent(generatePendingIntent("Notification Type"))
.setAutoCancel(true)
.setOngoing(true/false); // this is where you have to set
you can use Notification.Builder too but with NotificationCompat.Builder and in Android 4.1 and later you able to set action buttons depend on expanded notifications.
And to handle how what's to happen when click on notification method :
private PendingIntent generatePendingIntent(String type) {
PendingIntent pendingIntent;
Intent notificationIntent;
if (type.equals("link")) {
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
} else {
notificationIntent = new Intent(context, DesiredActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent =
PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
}
return pendingIntent;
}

How to open a specific activity based on Notification

I am currently working on a reminders app in which the user gets a notification with the name of the reminder and is then redirected to an activity which contains the text of the reminder in detail.
I am however, only able to redirect to the same activity each time.
I am using this code :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
So this redirects to the MainActivity on clicking the notification. I would like to redirect to a separate screen and then based on a key value display a text on that activity.
How do I achieve this ?
Thanks
Just change the PendingIntent using another Activity and/or append extra information on the Intent you are using to create the PendingIntent:
Intent launchIntent = new Intent(this, AnotherActivity.class)
launchIntent.putExtra("myKey", "myValue");
//....
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent , 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
And than, in you Activity's onCreate():
//...
getIntent().getStringExtra("myKey")
//do your stuff..
Just pass the value in the intent . eg.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
contentIntent.putExtra("phone", value);
contentIntent.putExtra("name", value);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Question has already been anwered and accepted, but here's another method:
int mId = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Content")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setNumber(1);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(mId, builder.build());

More than one button with pending intent on notification with remoteView

I have a notification using a remoteView. On the notification I have 3 buttons, each of them with their own pendingIntent. For some reason only the last pendingIntent that I add with setOnClickPendingIntent works.
Here is some of the
Intent intentPause = new Intent(context, BluetoothConnectionService.class);
intentPause.putExtra("pause", device.device.getAddress());
Intent intentIncrease = new Intent(context, BluetoothConnectionService.class);
intentIncrease.putExtra("inc", device.device.getAddress());
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 0, intentIncrease, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
expandedView.setOnClickPendingIntent(R.id.noti_pause_heat_button, pendingIntentPause);
expandedView.setOnClickPendingIntent(R.id.noti_inc_heat_button, pendingIntentIncrease);
Notification notification = new Notification.Builder(context)
.setContentTitle(this.device.getName())
.setSmallIcon(R.drawable.inu_small)
.setContentText("" + this.notificationId)
.setContent(expandedView)
.build();
use a different requestCode (second parameter of getService) for each PendingIndent. E.g.
PendingIntent pendingIntentPause = PendingIntent.getService(context, 0, intentPause, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentIncrease = PendingIntent.getService(context, 1, intentIncrease, PendingIntent.FLAG_UPDATE_CURRENT);

Categories

Resources