public void addnotification() {
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(
this);
Intent intent = new Intent(this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mbuilder.setSmallIcon(R.mipmap.ic_launcher);
mbuilder.setContentTitle("Notification");
mbuilder.setContentText("hi there");
mbuilder.setContentIntent(pendingIntent);
mbuilder.setOngoing(true);
mbuilder.addAction(R.mipmap.ic_launcher, "close", pendingIntent);
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mbuilder.build());
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager)
getApplicationContext().getSystemService(ns);
nMgr.cancel(0);
finish();
}
关闭按钮会关闭所有通知,但在关闭通知时会再次打开应用程序。
我想要的是当我点击它时它应该删除通知但不应该打开应用程序,它应该保持在同一个屏幕上。