I am trying to develop an app that will make notifications when you exit. But I want that when you touch the notification and you reopen the application, the notification disappears. How i can do it?
My code:
public class MainActivity extends Activity {
NotificationCompat.Builder mBuilder;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main_land);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setContentTitle("Mensaje de Alerta")
.setContentText("Ejemplo de notificación.")
.setContentInfo("4")
.setTicker("Alerta!");
}
public void salir(View view) {
finish();
Intent notIntent =
new Intent(MainActivity.this, MainActivity.class);
PendingIntent contIntent =
PendingIntent.getActivity(
MainActivity.this, 0, notIntent, 0);
mBuilder.setContentIntent(contIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify("NOTIFICATION", 0, mBuilder.build());
}
}
This code only makes the Notification and it opens the app again.
Using flags
Using Notification builder
Using Notification Manager
for API level 18 and above