
How to assign a custom method to a button in a notification
Creating buttons in the notification, you can’t just assign listeners to them, as we used to do when editing the user interface. The main way to assign actions in a notification is Intent - intent.
And if in order to assign a button to a transition to any Activity, it’s enough to simply create the corresponding intent, inside which the necessary action will be described, namely where and where we go, then in our case it will be necessary to do the following: assign the button to the button, give him an Action to work with the intent filter, create a BroadcastReciver, which will catch our Intent and even then execute the method we need.
So, for starters, let's create our notification. In my example, I put all of the following actions into a separate sendNotification method. But first, we need to create a line in which we write the key so that our receiver can catch our Intent. Since many applications constantly inject intents into the system, this key must be unique
Now you need to create a BroadCastReciver receiver. To do this, right-click on your folder app-> New-> Other-> BroadcastReciver. Next, give it a name, and in the file that opens, we need to remove the stub in the form of throwing an exception by writing our method in its place
But besides this, we need to configure the Intent Filter for our receiver in order to make it clear which Inetnt it should receive. To do this, go to the manifest and code block of our receiver, write Intent Filter, where we write the contents of the string variable BROADCAST_ACTION created at the very beginning
That's all, now if you click on the button we created in the notification, the method defined in the BroadcastReciver body will be launched
And if in order to assign a button to a transition to any Activity, it’s enough to simply create the corresponding intent, inside which the necessary action will be described, namely where and where we go, then in our case it will be necessary to do the following: assign the button to the button, give him an Action to work with the intent filter, create a BroadcastReciver, which will catch our Intent and even then execute the method we need.
So, for starters, let's create our notification. In my example, I put all of the following actions into a separate sendNotification method. But first, we need to create a line in which we write the key so that our receiver can catch our Intent. Since many applications constantly inject intents into the system, this key must be unique
//Создаем уникальный ключ
String BROADCAST_ACTION = "com.example.uniqueTag";
public void sendNotification () {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
Intent intent = new Intent(BROADCAST_ACTION);
//В следующей строке мы определяем кто именно будет обрабатывать наш Intent с помощью .getBroadcast
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentStopTrip, 0);
//Создаем уведомление
Notification BuilderNote =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_example)
.setContentTitle("Example title")
.setContentText("Example text")
.setContentIntent(pendingIntentPush)
//Далее мы создадим кнопку и присвоим ей наш pendingIntent
.addAction(R.mipmap.ic_example, "Button name", pendingIntent)
.build();
//Установим необходимые флаги
BuilderNote.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, BuilderNote);
}
Now you need to create a BroadCastReciver receiver. To do this, right-click on your folder app-> New-> Other-> BroadcastReciver. Next, give it a name, and in the file that opens, we need to remove the stub in the form of throwing an exception by writing our method in its place
public class ExampleReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
exampleMethod();
}
}
But besides this, we need to configure the Intent Filter for our receiver in order to make it clear which Inetnt it should receive. To do this, go to the manifest and code block of our receiver, write Intent Filter, where we write the contents of the string variable BROADCAST_ACTION created at the very beginning
That's all, now if you click on the button we created in the notification, the method defined in the BroadcastReciver body will be launched