2017-10-06 34 views
0

我想將多個進度通知合併到一個通知中。 Android的下載管理器究竟如何做到這一點。如果您從Chrome下載多個文件,則在下載時顯示爲一個通知。我該怎麼做呢?我有我的代碼在下面,但它顯示每個下載作爲一個單獨的通知。如何結合進度通知

主要

mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createNotificationChannel(); 
    mBuilder = new NotificationCompat.Builder(mActivity, "FileUpload") 
      .setSmallIcon(android.R.drawable.stat_sys_upload) 
      .setColor(ContextCompat.getColor(mActivity, R.color.colorNotification)) 
      .setContentTitle(new File(mFilePath).getName()) 
      .setContentText(mActivity.getResources().getString(R.string.app_name)) 
      .setGroup(NOTIFICATION_GROUP) 
      .setProgress(0, 0, true); 

    mNotifyManager.notify(2312, mBuilder.build()); 

OnProgress

@Override 
    public void onProgress(String filePath, double progress, long uploadedBytes, long totalBytes) { 
    mBuilder.setProgress(100, (int) (progress * 100), false); 
    mNotifyManager.notify(2312, mBuilder.build()); 
    } 

回答

0

docs

要設置通知,以便它可以被更新,以 通知ID發佈它通過調用NotificationManager .notify(ID, 通知)。要在發佈通知後更新此通知, 會更新或創建一個NotificationCompat.Builder對象,請從中建立一個 通知對象,然後發出與先前使用的ID相同的 ID。

要對通知進行分組,請嘗試以下操作。 創建一個組通知(KEY_NOTIFICATION_GROUP是這個組的字符串鍵):

Notification groupNotification = new NotificationCompat.Builder(this) 
     .setGroup(KEY_NOTIFICATION_GROUP) 
     .setGroupSummary(true) 
     // Customize the notification here. 
     .build(); 

然後通知添加到這個組是這樣的:

Notification notification = new NotificationCompat.Builder(this) 
     .setGroup(KEY_NOTIFICATION_GROUP) 
     // Customize the notification here. 
     .build(); 

Source

注意,在這種情況下,通知必須有不同的ID。

+0

這不會添加到像DownloadManager這樣的通知。這隻會使新的下載取代原來的。 –

+0

更新了答案。 –

+0

我已經在那裏有組,你可以在我的代碼中看到 –