Я использовал несколько ответов, которые я нашел здесь, в Stack Overflow, но они не сработали. Но в основном, мне нужно мое уведомление, чтобы сделать две вещи. Во-первых, мне нужно, чтобы он снова открывал приложение, когда было нажато само уведомление, и мне нужно, чтобы оно закрыло уведомление при нажатии AddAction.Android Notification не закрывается при AddAction, нажмите
Уведомление открывает приложение, когда оно нажимается, и это правильно, но когда я нажимаю AddAction («done»), он делает то же самое. Вместо действия, закрывающего уведомление, оно открывает приложение, как и само уведомление. Что может пойти не так?
public void onInput(MaterialDialog dialog, CharSequence input) {
//notification body
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
new Intent(getApplicationContext(), MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
//Rest of Notification
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(input.toString())); //BigText
builder.setOngoing(true); //Make persistent
builder.setContentIntent(pendingIntent); //OnClick for Reopening App
builder.setSmallIcon(R.drawable.ic_note);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Remember!");
builder.setContentText(input.toString()); //Get text from dialog input
Intent closeIntent = new Intent(getApplicationContext(), MainActivity.class);
closeIntent.putExtra(getPackageName(), NOTIFICATION_ID);
PendingIntent closeBtn = PendingIntent.getActivity(getApplicationContext(), 0, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.ic_action_name, "Done", closeBtn); //Action for the closer
notificationManager.notify(NOTIFICATION_ID, builder.build());
//toast
Toast.makeText(MainActivity.this, "Done! Reminder has been set. Check your Notification Bar! :)",
Toast.LENGTH_LONG).show();
//Close app when done entering in text
finish();
}
Причина, по которой я добавил кнопку, заключается в том, что уведомление является постоянным и никогда не может/должно быть удалено. Здесь я столкнулся с проблемой, так как я не мог понять, как заставить ее работать, сохраняя постоянное уведомление – MJonesDev