2016-06-04 4 views
0
public void createNotification(
    String title, String fileName, 
    boolean setProgressBar, 
    int maxValue, int progress, int notificationId, 
    boolean firstTime) { 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    if (firstTime) { 
     builder.setContentText(fileName); 
     builder.setSmallIcon(R.drawable.ic_launcher); 
     builder.setAutoCancel(false); 
    } 
    builder.setContentTitle(title); 
    if (setProgressBar) { 
     builder.setProgress(maxValue, progress, false); 
    } 
    Notification notification = builder.build(); 
    NotificationManager notificationManager = 
     (NotificationManager) 
     context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(notificationId, notification); 
} 

다른 활동에서 알림을 만들고 다른 활동에서 업데이트하고 있습니다. 다음과 같이 업데이트합니다 :업데이트시 알림 표시 줄이 사라집니다.

notificationManager.createNotification(
    "Downloading", document.getName(), 
    true, 
    (int) document.getContentLength(), (int)completedBytes, 
    (int) document.getContentLength(), 
    false); 

나는 매우 새로운 기능입니다. 모든 단서가 도움이 될 것입니다. 감사.

+0

수정 된 코드 형식; minor smithing – Prune

답변

0

업데이트 할 때마다 알림을 완전히 다시 작성해야하므로 createNotification() 방법에서 firstTime 수표를 삭제해야합니다.

편집 : 모든 통지는 콘텐츠 제목과 텍스트뿐만 아니라 작은 아이콘을 필요로하기 때문에 안드로이드 개발자 notifications 페이지에 표시된대로 자세한 설명에 관심있는 사람들을 위해은 업데이트 알림, 표시되지 않습니다. 업데이트 된 알림이 이 아니기 때문에 (firstTime이 false 인)에 포함되어 있지 않으므로 결과 알림이 유효하지 않습니다. 동일한 ID를 사용하여 NotificationManagernotify()을 호출해도 알림이 대체되지만 잘못된 알림이므로 새 알림이 표시되지 않습니다.

편집 (16/06/21) : 다음 클래스는 진행률 알림을 만들고 업데이트하는 데 사용할 수 있으며 업로드 한 파일을 기반으로합니다. 알림을 업데이트하려면 createNotification(...)으로 다시 전화하면 알림이 올바르게 변경됩니다.

public class SDCNotificationManager { 
    public final static String OPEN_NOTIFICATION = "notification_open"; 
    public final static int MAX_PROGRESS_VALUE = 100; 

    /* 
    * Creates or updates a progress notification 
    */ 
    public static void createNotification(Context context, String title, String fileName, int currentProgress, int maxProgress, int notificationId) { 

     boolean notificationEnabled = SettingsManager.getInstance().(SettingsKey.ENABLE_NOTIFICATION); 
     if (notificationEnabled) { 
      Intent intent = new Intent(context, MainActivity.class); 
      intent.setAction(OPEN_NOTIFICATION); 
      // build notification 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
      builder.setContentTitle(title); 
      builder.setContentText(fileName); 
      builder.setSmallIcon(R.drawable.ic_launcher); 
      builder.setProgress(maxProgress, currentProgress, false); 
      Notification notification = builder.build(); 
      // send notification to system service 
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(notificationId, notification); 
     } 
    } 
} 
+0

새로운 Notification.Builder를 만들 때마다 동일한 알림을 업데이트 할 수 없었기 때문에. 그래서 나는 건축업자를 정적으로 만들었고 효과가있었습니다. 이유를 모르겠다. 설명 할 수 있습니까? –

+0

업데이트 된 코드를 게시 해주십시오. 자세히 살펴볼 수 있습니다. 또한 알림을 제대로 업데이트하려면 동일한 알림 ID를 사용해야하므로 'document.getContentLength()'가 최선의 옵션이 아닐 수 있습니다. – TR4Android

+0

내 코드를 업로드 할 수없는 나는, 내가 파일을 업로드 한 오류 –

관련 문제