我用一些答案,我發現這裏堆棧溢出,但他們沒有奏效。但基本上,我需要通知我做兩件事。一,我需要它在通知本身被點擊時再次打開應用程序,並且當點擊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