Reputation: 1994
following this guide and other discussions online I've made this pice of code that should show with the .addAction method the buttons in my notification:
@SuppressLint("NewApi")
public void generateNotificationSong()
{
if(android.os.Build.VERSION.SDK_INT >= 11){
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(songsList.get(currentSongIndex).getTitle());
builder.setContentText("Next: " + songsList.get(getNextSong()).getTitle());
builder.setOngoing(true); //E' in corso
builder.setAutoCancel(true);
//intent con activity che devo aprire (la corrente)
Intent thisintent = getIntent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, thisintent, PendingIntent.FLAG_UPDATE_CURRENT);
//Aggiungo i bottoni alla notifica se android è > 4.1
if(android.os.Build.VERSION.SDK_INT >= 16)
{
Log.v("A", "CIAO");
Intent prevIntent = new Intent();
prevIntent.setAction("prec");
PendingIntent pendingPrev = PendingIntent.getBroadcast(this, 123, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent nextIntent = new Intent();
nextIntent.setAction("next");
PendingIntent pendingNext = PendingIntent.getBroadcast(this, 123, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent();
pauseIntent.setAction("pause");
PendingIntent pendingPause = PendingIntent.getBroadcast(this, 123, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.btn_previous, "Previous", pendingPrev);
builder.addAction(R.drawable.btn_pause, "Pause", pendingNext);
builder.addAction(R.drawable.btn_next, "Next", pendingPause);
}
//la setto alla notifica
builder.setContentIntent(pendingIntent);
//infine la chiamo
mNotify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(android.os.Build.VERSION.SDK_INT >= 16)
mNotify.notify(001, builder.build()); //funziona meglio
else
mNotify.notify(001, builder.getNotification()); //deprecated
}
}
What I get is my notification without the buttons I've added, I think I'm missing something but I've not found a solution yet
Upvotes: 1
Views: 2952
Reputation: 161
Here is a possible cause. Android may hide the buttons when there are other notifications present, especially when your phone is being charged. Try tap your notification with two fingers and swipe down. You should be able to see your buttons.
Upvotes: 8