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)