2016-11-23 1 views
1

서버에서 메시지를받는 응용 프로그램에서 작업하고 있습니다. 메시지를 받으면 알림이 나타납니다. 두 번째 메시지를 받으면 완전히 새로운 알림을 만드는 대신 스택해야합니다.Android 알림이 스택되지 않습니다.

메시지를 수신 할 때 실행할 방법이있는 인터페이스를 만들었습니다.

Server 개체는 메시지를 수신하는 곳이며, 생성자는 위에서 언급 한 인터페이스를 허용합니다.

서버 개체를 초기화 할 때 재정의 된 메서드로 알림을 만드는 수신기 인터페이스의 새 인스턴스를 전달합니다. 새로운 알림이 생성 될 때마다 나는 NEW_POST_NOTI 정수를 1 씩 증가시키고 그룹에 추가한다고 생각합니다. 같은 코드는 메시지가 수신 될 때마다 실행되는 것을

final int PUSHES_GROUP = 67; 
int NEW_POST_NOTI = 56; 

...

Server server = new Server((msg) -> { 
    nm = NotificationManagerCompat.from(ctx); 
    Notification noti = new NotificationCompat.Builder(ctx) 
     .setSmallIcon(R.drawable.ic_noti) 
     .setContentTitle("New Message") 
     .setContentText(msg) 
     .setGroup(PUSHES_GROUP) 
     .build(); 
    nm.notify(NEW_PUSH_NOTI++, noti); 
}); 

있지만 그룹화하는 대신 각 메시지에 대해 별도의 통지를 작성합니다

내 코드는 다음과 같습니다 . 또한 setStyle을 사용하여 InboxStyle으로 만들었지 만 동적으로 알림을 추가하는 방법을 잘 모르겠습니다. 내 논리에 문제가 있습니까? 아니면 단순히 Notification API를 잘못 사용하고 있습니까?

+0

그룹화는 android wear입니다. 안드로이드 핸드 헬드의 경우 라인 및 요약과 함께받은 편지함 스타일을 사용하십시오 : https://developer.android.co.kr/training/wearables/notifications/stacks.html # AddSummary – njzk2

+0

당신이 옳았습니다. 그럼에도 불구하고 상상하기는 쉽지 않지만 상당히 간단합니다. – Plays2

답변

2

물품. 그런 다음 앱에서 onResume을 호출하면 InboxStyle을 재설정합니다. 그래서 예를 들면

는 :

public class ServerService extends Service { 
    ... 
    NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); 
    private static NotificationManagerCompat nm; 
    private final Context ctx = Server.this; 
    Server server; 
    private static int pendingPushes = 0; 
    private final int NEW_PUSH_NOT = 2; 
    ... 
    @Override 
    public int onStartCommand(Intent i, int f, final int s) { 
     nm = NotificationManagerCompat.from(ctx); 
     try { 
      server = new Server((msg) -> { 
       pendingPushes++; 
       style.setBigContentTitle(pendingPushes +" new pushes");      
       style.addLine(msg); 
       Notification noti = new NotificationCompat.Builder(ctx) 
         .setSmallIcon(R.drawable.ic_noti) 
         .setStyle(style) 
         .setGroupSummary("Click here to view") 
         .setNumber(pendingPushes) //Should make the number in bottom right the amount of pending messages but not tested yet 
         .build(); 
       nm.notify(NEW_PUSH_NOT, noti); 
      }); 
      server.start(); 
     } catch(IOException e) { 
      e.printStackTrace(); 
     } 
     return START_STICKY; 
    } 

은 그 때 나는 보류중인 수를 다시 시작하고 알림을 닫을 수있는 방법을 만들었습니다. 나는 onResume()

public static void resetPendingPushes() { 
    pendingPushes = 0; 
    style = new NotificationCompat.InboxStyle(); 
    if (nm != null) { 
     nm.cancel(NEW_PUSH_NOT); 
    } 
} 

MainActivity의 내부에 내 MainActivity에 당신은 무리 도움이 대답 모든 사람에게

@Override 
protected void onResume() { 
    super.onResume(); 
    ServerService.resetPendingPushes(); 
} 

감사합니다 그것을 실행! 비슷한 질문을 가진 사람에게 미안 미안하지만 내 대답에 오타가있을 경우 셀에서 오히려 빨리 입력했습니다.

1

을 사용 하시어 NotificationManager에서 사용하는 것이 좋습니다. 이 NotificationID는 기본적으로 응용 프로그램마다 고유 한 ID를 나타내므로 동일한 알림 ID를 사용하면 모든 알림을 받아 볼 수 있습니다. 다음을 시도해보고 알려주십시오.

static final int MY_NOTIFICATION_ID = 1; 

그런 정적 알림 ID를 선언하십시오. 그리고 같은 통보! 그래서 대신

nm.notify(NEW_PUSH_NOTI++, noti); 

당신은 대답은 새 메시지가 수신 될 때마다 InboxStyle 인스턴스 변수를 만들고, 그 위에 addLine 전화를했다

nm.notify(MY_NOTIFICATION_ID, noti); 
+0

효과가 있었나요? – San

+0

그냥 아니 었어. 내 대답을 확인해. – Plays2

관련 문제