How to show buttons in notification area
Introduction
In my test project *1, I wanted to show some buttons in notification area of the Android device. So I googled for a while and I found that Notification.Builder#addAction *2 might help me.
Result
I implemented a button on notification area like below.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); if(event.getArtwork() != null) { builder.setLargeIcon(event.getArtwork()); } builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle(event.getTitle()); builder.setContentText(event.getArtist()); builder.setSubText(event.getAlbum()); Intent intent = new Intent(this, PlaybackService.class); intent.setAction(ACTION_PAUSE); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(android.R.drawable.ic_media_pause, "PAUSE", pendingIntent); intent = new Intent(this, SimpleMusicPlayer.class); pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); startForeground(R.id.notification_id, builder.build());
However any button did not show at all and Logcat also did not tell me anything (no error log!) although I was using Galaxy Nexus (Android 4.1.1) so a button on notification area should show according to the official online manual.
More research
I googled about the problem and I found the very useful code *3 . First I copied this code to my project and tested it. As a result, I could see a button on notification area! But why?
Second I rolled my project back to the original and modified my code like below.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setAutoCancel(false); builder.setDefaults(Notification.DEFAULT_ALL); builder.setWhen(System.currentTimeMillis()); builder.setContentTitle(title); builder.setSmallIcon(R.drawable.ic_launcher); builder.setTicker(title); builder.setPriority(NotificationCompat.PRIORITY_HIGH); Intent intent = new Intent(this, SimpleMusicPlayer.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); Intent actionIntent = new Intent(this, PlaybackService.class); PendingIntent actionPendingIntent = PendingIntent.getService(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(android.R.drawable.ic_media_pause, "PAUSE", actionPendingIntent); // if(artwork != null) { // builder.setLargeIcon(artwork); // } // builder.setContentText(artist); // builder.setSubText(album); // startForeground(R.id.notification_id, builder.build()); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.nontify(R.id.notification_id, builder.build());
After I made sure that this code worked correctly, I commented out the parts which were not in my original code one by one. After some tests, I found that when setPriority method call was commented out, the button disappeared.
Conclusion
If you want to use buttons on notification area, I should call NotificationCompt.Builder#setPriority with NotificationCompat.PRIORITY_HIGH as a parameter.
*1:https://github.com/yohpapa/simplemusicplayer
*2:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#addAction%28int,%20java.lang.CharSequence,%20android.app.PendingIntent%29
*3:https://github.com/commonsguy/cw-omnibus/blob/master/Notifications/BigNotify/src/com/commonsware/android/bignotify/MainActivity.java