2015-01-09 2 views
0

알림 창에서 알림 내용을 다시 정렬하지 않고 알림 내용을 업데이트하는 데 문제가 있습니다.통지의 내용을 적절하게 업데이트하는 방법은 무엇입니까?

notification IDTAG이 같으면 알림이 대체되어야한다고 공식 설명서에 나와 있습니다. 그리고 그것은 정확히 하나의 문제를 제외하고는 어떤 일이 일어나는 것입니까? 우선 순위를 LOW 또는 MIN으로 설정하지 않는 한, 목록을 업데이트 할 때마다 목록 상단으로 밀려납니다.

setDefaults()을 사용하여 빌더에 기본값을 설정하지 않고 자동 알림을 게시하면 트릭이 실행되지만 원하는 것은 아닙니다.

내가 뭘 잘못하고 있니? Gmail 앱이 자체를 재정렬하지 않고 알림을 '실행 취소'로 변경할 수 있기 때문에 이것이 가능하다는 사실을 알고 있습니다. 나는 안드로이드가 다시 한번보기를 다시 구축하는 귀하의 경우 추측하고

void showFirstNotif(){ 

    mNotifBuilder.setContentTitle("First") 
      .setContentText("Body") 
      .setWhen(activityStartTime) 
      .setSmallIcon(PhotoUtils.getAppIconResId()) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setOnlyAlertOnce(true); 

    NotificationManagerCompat.from(this).notify("Test", NOTIF_ID, mNotifBuilder.build()); 

} 

void showSecondNotif(){ 

    mNotifBuilder.setContentTitle("Second") 
      .setContentText("Body") 
      .setWhen(activityStartTime) 
      .setSmallIcon(PhotoUtils.getAppIconResId()) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setOnlyAlertOnce(true); 

    NotificationManagerCompat.from(this).notify("Test", NOTIF_ID, mNotifBuilder.build()); 

} 

답변

1

:

여기 내가 사용하는 코드입니다. 메시지가있는 updateNotification(String)으로 전화하면 사용자를 괴롭 히거나 Notification을 다시 정렬하지 않고 알림을 업데이트합니다.

예를 만들었으므로 완벽하게 작동합니다. 같은 알림 ID를 사용하고 있지만이 경우

private void updateNotification(String message) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
       MainActivity.this); 
     builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle(
       "Notification").setDefaults(Notification.DEFAULT_ALL); 
     builder.setContentText(message); 
     mNotifyManager.notify(mNotificationId2, builder.build()); 
    } 

하지만 새로운 Notification 자체 인 것처럼 그것은 재건 따라서 상단에 푸시 : 당신이 사용하는 경우

public class MainActivity extends Activity { 

    private NotificationManager mNotifyManager; 
    private NotificationCompat.Builder mBuilder; 
    private int mNotificationId1 = 1; 
    private int mNotificationId2 = 2; 
    private int mNotificationId3 = 3; 
    private int mNotificationId4 = 4; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mBuilder = new NotificationCompat.Builder(MainActivity.this); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 1"); 
     mNotifyManager.notify(mNotificationId1, mBuilder.build()); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 2"); 
     mNotifyManager.notify(mNotificationId2, mBuilder.build()); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 3"); 
     mNotifyManager.notify(mNotificationId3, mBuilder.build()); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 4"); 
     mNotifyManager.notify(mNotificationId4, mBuilder.build()); 

     findViewById(R.id.button).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       updateNotification("Notification 2 changed silently"); 
      } 
     }); 
    } 

    private void updateNotification(String message) { 
     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setDefaults(Notification.DEFAULT_ALL); 
     mBuilder.setContentText(message); 
     mNotifyManager.notify(mNotificationId2, mBuilder.build()); 
    } 
} 

: 보라 .

+0

아니요. 이미 업데이트해야하는 속성 만 설정하여이 작업을 이미 시도했습니다. 작동하지 않습니다 : ( – Saket

+0

Ohkay, 감사합니다. – Saket

+0

@Saket 내 대답을 업데이트했습니다.보세요 – CodeWarrior

관련 문제