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