1

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

回答

0

只需添加builder.autoCancel(true);

這將解決您的問題。

0

此代碼您有:

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 

把標有 「完成」 的動作,將調用startActivity()closeIntent(其中啓動您的應用)。它確實如此。

用戶已經知道如何滑動通知來刪除它,而無需執行任何操作。你爲什麼還要在通知中添加一個額外的動作按鈕來完成這個動作?看起來對我來說過分了。

如果雷爾想這樣,你可以嘗試的動作使用空PendingIntent,因爲基本上你想要什麼,當用戶單擊該按鈕時發生:

builder.addAction(R.drawable.ic_action_name, "Done", 
     null); //Action for the closer 
+0

我添加按鈕的原因是因爲通知是持久的,永遠不能/不應該被刷掉。這是我遇到問題的地方,因爲我無法弄清楚如何在保持通知持續的情況下工作 – MJonesDev