0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    button = (Button) findViewById(R.id.button); 
    final Intent intent = new Intent(MainActivity.this, AlertActivity.class); 
    final PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this); 
      builder.setContentText("Click here"); 
      builder.setSmallIcon(R.mipmap.ic_launcher); 
      builder.setContentTitle(getString(R.string.app_name)); 
      builder.addAction(R.mipmap.ic_launcher, "Test", pendingIntent); 
      builder.setAutoCancel(true); 
      NotificationManagerCompat.from(MainActivity.this).notify(0, builder.build()); 
     } 
    }); 
} 

또한 builder.addAction()을 제거하여 실험했지만 알림을 클릭해도 아무 효과가 없습니다. 조치를 추가하지 않고 통지를 클릭하면 어떻게 사용자를 특정 활동으로 유도 할 수 있습니까? 또한, 두 경우 모두; 수동으로 슬라이드하여 제거하지 않으면 알림을 닫을 수 없습니다.builder.setAutoCancel (true) 작동하지 않습니다.

답변

5

AUTOCANCEL 단지가 기본으로하면 작동을 t 알림 클릭은 당신이 당신의 케이스 ID에 노 티피 케이션 인스턴스에 대한 통지 자기

전화 명확한 (INT ID)를 취소 당신이 버튼을 사용하는 경우 호출 0

+0

'notification.clear (id)'를 사용하는 방법과 위치를 보여줍니다. – Apurva

+0

onCreate();에서 알림 빌더를 직접 만들었습니다. 여전히 autoCancel이 작동하지 않습니다. clear()을 호출하는 방법? –

+0

알림 관리자에서 알림을 호출하면 ID (나중에 알림에 액세스 할 때 사용할 수있는 고유 ID)가 표시됩니다 (알림 관리자에서 제공). notify (int id, 알림 알림) 취소하려면 동일한 ID로 을 취소 할 수 있습니다. 기본적으로 ID를 추적하거나 ID를 번들에 넣어야합니다. –

3

문제 해결해야 setContentIntent 사용 :

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); 

전체 코드는 :

button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this); 
      builder.setContentText("Click here"); 
      builder.setSmallIcon(R.mipmap.ic_launcher); 
      builder.setContentTitle(getString(R.string.app_name)); 
      builder.setAutoCancel(true) 
      builder.addAction(R.mipmap.ic_launcher, "Test", pendingIntent); 
      builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)); 
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(0, builder.build()); 
     } 
    }); 

이 업데이트 :

builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 
+0

setContentIntent() 작동합니다, 감사입니다. 그러나 우리가 어떤 행동을 추가하고 그 행동을 클릭 할 때와 정확하게 같은 방법으로 알림을 무시하는 방법은 무엇입니까? –

+0

@AseedUsmani 내 업데이트 대답을 참조하십시오. – Ironman

+0

작동하지 않습니다. builder.getNotification(). flags | = Notification.FLAG_AUTO_CANCEL; –

관련 문제