2017-12-31 7 views
1

수신자 중 한 곳에서 설정 한 로컬 알림이 있습니다. 알림 클릭시 특정 활동을 열려고합니다. 아래는 제 코드입니다.로컬 알림 클릭하여 활동 열기

NotificationCompat.Builder builder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("Its Your day!!") 
        .setContentText(message) 
        .setAutoCancel(true); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(contentIntent); 
    // Add as notification 
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(times, builder.build()); 

하지만 작동하지 않습니다. On 알림을 클릭하면 상태 표시 줄에서 알림이 제거됩니다. 내가 놓친 게 있니?

+0

했나 앱을 닫은 후 시도 :

UPDATE 여기
샘플 프로젝트입니다. SINGLE_TOP은 MainActivity가 살아 있다면'onNewIntent()'에 인 텐트를 전달합니다. – ADM

답변

0

Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP을 '의도'에 Flag으로 추가하십시오. 마찬가지로 -

notificationIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

또한 통지에 대한 고유 ID를 제공합니다. 같은 -도에

int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNotificationId , notificationIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

-

manager.notify(uniqueNotificationId , builder.build()); 
+0

'PendingIntent.FLAG_UPDATE_CURRENT'는 같은'requestCode'에서만 의미가 있습니다. 각각의'PendingIntent'에 대한 유일한 ID는 매번 새로운'PendingIntent'를 만듭니다. –

1

이 시도 : 대신의

Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

을 :

Intent notificationIntent = new Intent(context, MainActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

사용이 코드 알림 빌더 코드 앞에.

이제이 전체 버전입니다 :

Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

NotificationCompat.Builder builder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("Its Your day!!") 
        .setContentText(message) 
        .setAutoCancel(true) 
        .setContentIntent(pendingIntent); 

NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(times, notificationBuilder.build()); 

시도는이를 사용 할 수 있습니다. 나는 네가 네가 원하는 대답을 얻길 바랍니다. https://github.com/ImtiazDipto01/SimpleNotification

+0

아직도 작동하지 않습니다 –

+0

내 편집 된 버전을 확인하십시오 .. 그래서 당신을 도울 것입니다. –

+0

@ madhuriHR 업데이트 섹션을 확인하고 샘플 프로젝트를 추가했습니다. 문제가 생기면 알려주세요. –