【发布时间】:2016-12-03 22:50:41
【问题描述】:
我使用了一些在 Stack Overflow 上找到的答案,但没有奏效。但基本上,我需要我的通知来做两件事。一,我需要它在单击通知本身时再次打开应用程序,并且我需要它在单击 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();
}
【问题讨论】:
标签: android android-intent notifications android-pendingintent