1

Stack Overflow에서 찾은 답변을 사용했지만 제대로 작동하지 않았습니다. 그러나 근본적으로, 나는 두 가지를하기 위해 나의 통지가 필요하다. 하나는 알림 자체를 클릭 할 때 앱을 다시 열어야하며 AddAction을 클릭하면 알림을 닫아야합니다.안드로이드 알림이 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 

closeIntentstartActivity()을 (당신의 응용 프로그램을 실행하는)를 호출 할 것이다 "완료"로 표시하는 행동을 넣습니다. 그것은 정확히 그것이해야 할 일을합니다.

사용자는 아무 것도하지 않고 알림을 스 와이프하여 삭제하는 방법을 이미 알고 있습니다. 왜 그런 작업을 수행하기 위해 알림에 추가 작업 버튼을 추가해야합니까? 과도한 태도로 보입니다. 당신이 원하는 reall 경우 기본적으로 사용자가 버튼을 클릭 할 때 일이 아무 것도 원하지 않기 때문에

, 당신은 작업에 널 PendingIntent를 사용을 시도 할 수 있습니다 : 단순히 추가

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

버튼을 추가 한 이유는 알림이 지속적이고 결코 스왑 할 수 없기 때문입니다. 알림을 지속적으로 유지하면서 작동시키는 방법을 파악할 수 없기 때문에 문제를 해결할 수 있습니다. – MJonesDev