我用一些答案,我发现这里堆栈溢出,但他们没有奏效。但基本上,我需要通知我做两件事。一,我需要它在通知本身被点击时再次打开应用程序,并且当点击AddAction时我需要它关闭通知。Android通知不关闭AddAction点击
通知打开应用程序,当它被点击,这是正确的,但是当我点击AddAction(“完成”),它做同样的事情。与关闭通知的操作不同,它会像通知本身一样打开应用程序。可能会出现什么问题?
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