'Android Notification Action Button Click Listener
I am developing an event planning application, and I'm currently working on a feature for assigning an item to a guest. When the item is assigned, a notification is pushed to the guest, asking him whether or not he can bring the required item. Depending on the input of the user, an HTTP request is sent back to the server, updating the status of item (bringing/not bringing).
I'm kind of stuck on responding to the click on the notification action buttons, since I didn't quite understand how it works. I managed to show the action buttons, but couldn't figure out how to respond to clicking on them. As I've seen online, the response to the notification action buttons, is managed by the PendingIntent, which I couldn't manage to understand how it works, and how I can use it to send HTTP requests according to the button clicked.
I'd be glad for some help and advice.
[This is an example of the notification sent to the user.][
This is the method that notifies the user:
private void notifyUser(Bundle data) {
...
//Notification configuration.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(...)
.setLargeIcon(...)
.setContentTitle(...)
.setContentText(...)
.setAutoCancel(...)
.setSound(...);
//Get the item data from the request sent to the GCM server.
Item item = GsonFactory.getGson()
.fromJson(data.getString("data"), Item.class);
//This is the part that I do not understand...
notificationBuilder
.addAction(R.drawable.ic_close_black, "No", null)
.addAction(R.drawable.ic_done_black, "Yes", null);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Thank you in advanced!
Solution 1:[1]
I hoope this can help you
//Exemple of notification with Button
private static void scheduleNotificationWithButton(Context context, String message) {
int notifReCode = 1;
//What happen when you will click on button
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
//Button
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();
//Notification
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Back to Application ?")
.setContentTitle("Amazing news")
.addAction(action) //add buton
.build();
//Send notification
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
Solution 2:[2]
You have to set a PendingIntent
object to your actions so that when the user performs any action on the notification buttons that particular intent gets fired.
The PendingIntent may be set for a Activity, Broadcast Receiver or Service
.
If you want to show an activity when the user clicks the notification button then you can use -
Intent intent = new Intent(this, NotificationReceiverActivity.class);
intent.putExtra(<key>, item ); //If you wan to send data also
PendingIntent pIntent = PendingIntent.getActivity(this, <notification_id>, intent, 0); //<notification_id> may be any number
And set this intent to your notification actions -
notificationBuilder
.addAction(R.drawable.ic_close_black, "No", pIntent)
.addAction(R.drawable.ic_done_black, "Yes", pIntent);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | devesh |
Solution 2 | Shadab Ansari |