Android 单击 AddAction 时通知未关闭
Android Notification not closing on AddAction click
我使用了在 Stack Overflow 上找到的一些答案,但它们没有用。但基本上,我需要我的通知来做两件事。第一,我需要它在单击通知本身时再次打开应用程序,并且我需要它在单击 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();
}
您拥有的代码:
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
放置一个标记为 "Done" 的操作,它将调用 startActivity()
和 closeIntent
(启动您的应用程序)。它做的正是它应该做的。
用户已经知道如何在不执行任何操作的情况下滑动通知将其删除。你为什么要费心在通知中添加一个额外的操作按钮来完成这个呢?对我来说似乎太过分了。
如果你真的想要这个,你可以尝试在操作上使用 null PendingIntent
,因为基本上你不希望用户单击按钮时发生任何事情:
builder.addAction(R.drawable.ic_action_name, "Done",
null); //Action for the closer
只需添加builder.autoCancel(true);
这将解决您的问题。
我使用了在 Stack Overflow 上找到的一些答案,但它们没有用。但基本上,我需要我的通知来做两件事。第一,我需要它在单击通知本身时再次打开应用程序,并且我需要它在单击 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();
}
您拥有的代码:
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
放置一个标记为 "Done" 的操作,它将调用 startActivity()
和 closeIntent
(启动您的应用程序)。它做的正是它应该做的。
用户已经知道如何在不执行任何操作的情况下滑动通知将其删除。你为什么要费心在通知中添加一个额外的操作按钮来完成这个呢?对我来说似乎太过分了。
如果你真的想要这个,你可以尝试在操作上使用 null PendingIntent
,因为基本上你不希望用户单击按钮时发生任何事情:
builder.addAction(R.drawable.ic_action_name, "Done",
null); //Action for the closer
只需添加builder.autoCancel(true);
这将解决您的问题。