There is a sample code in which a notification and a button are created. And by clicking on the button in the notification, the Activity opens.

And how to make the opposite when you click on the activation button was closed and the notification itself disappeared?

public void sendActionNotification(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Намерение для запуска второй активности Intent intent = new Intent(this, SecondActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); // Строим уведомление Notification builder = new Notification.Builder(this) .setTicker("Пришла посылка!") .setContentTitle("Посылка") .setContentText( "Это я, почтальон Печкин. Принес для вас посылку") .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent) .addAction(R.drawable.ic_launcher, "Открыть", pIntent) .addAction(R.drawable.ic_launcher, "Отказаться", pIntent) .addAction(R.drawable.ic_launcher, "Другой вариант", pIntent) .build(); // убираем уведомление, когда его выбрали builder.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, builder); } 

stack trace:

 E/ActivityThread: Activity ru.mysite.Player has leaked IntentReceiver ru.mysite.Player$1@45b817e that was originally registered here. Are you missing a call to unregisterReceiver()? android.app.IntentReceiverLeaked: Activity ru.mysite.Player has leaked IntentReceiver ru.mysite.Player$1@45b817e that was originally registered here. Are you missing a call to unregisterReceiver()? at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1159) at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:946) at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1330) at android.app.ContextImpl.registerReceiver(ContextImpl.java:1310) at android.app.ContextImpl.registerReceiver(ContextImpl.java:1304) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:586) at ru.mysite.Player.onCreate(Player.java:152) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

    1 answer 1

      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent maybeReceive = new Intent(); maybeReceive.setAction(MAYBE_ACTION); PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); // Строим уведомление Notification builder = new Notification.Builder(this) .setTicker("Пришла посылка!") .setContentTitle("Играет: " + котенок) .setContentText("Это я, почтальон Печкин. Принес для вас посылку") .setSmallIcon(R.drawable.icon_play).setContentIntent(pIntent) .addAction(R.drawable.icon_stop, "ОК", pendingIntentMaybe) .build(); // убираем уведомление, когда его выбрали builder.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, builder); 

    In your onCreate()

     BroadcastReceiver br = new BroadcastReceiver() { // действия при получении сообщений public void onReceive(Context context, Intent intent) { finish(); } }; // создаем фильтр для BroadcastReceiver IntentFilter intFilt = new IntentFilter(MAYBE_ACTION); // регистрируем (включаем) BroadcastReceiver registerReceiver(br, intFilt); 

    MAYBE_ACTION - any unique string for the application.

    • And you can say what happens hereReally.setAction (MAYBE_ACTION); PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast (this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); and how does the BroadcastReceiver communicate with the button? - Collective Farmer
    • When I click the Stop button in the notification, an error is displayed in the log. Your question has been updated. - Collective Farmer
    • When you click a button, a message is sent with action MAYBE_ACTION. BroadcastReceiver can intercept messages to which it is subscribed. Accordingly, your BroadcastReceiver is subscribed to MAYBE_ACTION, it will catch the message. And the error is because you need to make unregisterReceiver (broadcastReceive); on onDestroy - pavel163
    • Thank. Got it. - Collective Farmer