I have service which plays the radio & creates a notification. The notification must incude title, text, time & a button(pause/play). I have added rest of the things & notification is getting displayed. But I'm not sure about how to add the button to the notification. On searching, I found I can use RemoteView, RemoteControlClient & MediaStyles. But RemoteControlClient & MediaStyles are used when there is a media file playing with all buttons(previous,pause,play,back). So I'm really confused about it. Can anyone suggest me, which one to use to add buttons in the notification for Lollypop.
Asked
Active
Viewed 2,469 times
3

Ashish Tamrakar
- 810
- 10
- 24

Sangeetha Pinto
- 1,022
- 3
- 14
- 32
-
1use remote views and follow the link http://www.androidbegin.com/tutorial/android-custom-notification-tutorial/ – Ramesh Feb 06 '15 at 05:03
-
Will it work for Lollypop? – Sangeetha Pinto Feb 06 '15 at 05:13
-
recently we implemented notification in one application with remote views.Its working fine in lollipop also – Ramesh Feb 06 '15 at 05:17
-
1use custom notification with remote views - http://www.skholingua.com/android-basic/advance-topics/notifications – chiru Feb 06 '15 at 05:25
-
Okay..I'll use it..& let u know if it works – Sangeetha Pinto Feb 06 '15 at 05:38
1 Answers
1
First you need to know addAction
is deprecated in 23 API, read this to relate
Call showNotification()
method to show notification
private void showActionButtonsNotification() {
Notification.Builder notif;
NotificationManager nm;
notif = new Notification.Builder(getApplicationContext());
notif.setSmallIcon(R.mipmap.ic_launcher);
notif.setContentTitle("Hi there!");
notif.setContentText("This is even more text.");
Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notif.setSound(path);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent yesReceive = new Intent();
yesReceive.setAction(MyNotificationReceiver.RESUME_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
notif.addAction(R.drawable.resume, "resume", pendingIntentYes);
Intent yesReceive2 = new Intent();
yesReceive2.setAction(MyNotificationReceiver.STOP_ACTION);
PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT);
notif.addAction(R.drawable.stop, "stop", pendingIntentYes2);
Intent maybeReceive2 = new Intent();
maybeReceive2.setAction(MyNotificationReceiver.CANCEL_ACTION);
PendingIntent pendingIntentMaybe2 = PendingIntent.getBroadcast(this, MyNotificationReceiver.REQUEST_CODE_NOTIFICATION, maybeReceive2, PendingIntent.FLAG_UPDATE_CURRENT);
notif.addAction(R.drawable.cancel, "cancel", pendingIntentMaybe2);
assert nm != null;
nm.notify(MyNotificationReceiver.REQUEST_CODE, notif.getNotification());
}
Create MyNotificationReceiver
class to recive the clicks
public class MyNotificationReceiver extends BroadcastReceiver {
public static int REQUEST_CODE_NOTIFICATION = 1212;
public static int REQUEST_CODE = 10;
public static final String RESUME_ACTION = "RESUME_ACTION";
public static final String STOP_ACTION = "STOP_ACTION";
public static final String CANCEL_ACTION = "CANCEL_ACTION";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("action", action);
if (intent.getAction() != null) {
switch (intent.getAction()) {
case RESUME_ACTION :
Toast.makeText(context, "resume", Toast.LENGTH_SHORT).show();
// you resume action
break;
case STOP_ACTION :
Toast.makeText(context, "stop", Toast.LENGTH_SHORT).show();
// you stop action
break;
case CANCEL_ACTION:
Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show();
// you cancel action
break;
}
}
}
}
Finally, add receiver
to your manifests
<receiver android:name=".MyNotificationReceiver">
<intent-filter>
<action android:name="RESUME_ACTION"/>
<action android:name="STOP_ACTION"/>
<action android:name="CANCEL_ACTION"/>
</intent-filter>
</receiver>

Mohamed Embaby
- 960
- 8
- 26