|
1 | 1 | package net.gotev.uploadservice; |
2 | 2 |
|
3 | 3 | import android.app.Notification; |
| 4 | +import android.app.NotificationChannel; |
4 | 5 | import android.app.NotificationManager; |
5 | 6 | import android.content.Context; |
6 | 7 | import android.content.Intent; |
7 | 8 | import android.media.RingtoneManager; |
| 9 | +import android.os.Build; |
8 | 10 | import android.os.Handler; |
9 | 11 | import android.support.v4.app.NotificationCompat; |
10 | 12 |
|
@@ -115,6 +117,16 @@ public UploadTask() { |
115 | 117 | */ |
116 | 118 | protected void init(UploadService service, Intent intent) throws IOException { |
117 | 119 | this.notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); |
| 120 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 121 | + String notificationChannelId = params.notificationConfig.getNotificationChannelId(); |
| 122 | + if (notificationChannelId == null) { |
| 123 | + notificationChannelId = UploadService.NAMESPACE; |
| 124 | + } |
| 125 | + if (notificationManager.getNotificationChannel(notificationChannelId) == null) { |
| 126 | + NotificationChannel channel = new NotificationChannel(notificationChannelId, "Upload Service channel", NotificationManager.IMPORTANCE_DEFAULT); |
| 127 | + notificationManager.createNotificationChannel(channel); |
| 128 | + } |
| 129 | + } |
118 | 130 | this.service = service; |
119 | 131 | this.mainThreadHandler = new Handler(service.getMainLooper()); |
120 | 132 | this.params = intent.getParcelableExtra(UploadService.PARAM_TASK_PARAMETERS); |
@@ -430,7 +442,7 @@ private void createNotification(UploadInfo uploadInfo) { |
430 | 442 |
|
431 | 443 | UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); |
432 | 444 |
|
433 | | - NotificationCompat.Builder notification = new NotificationCompat.Builder(service) |
| 445 | + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) |
434 | 446 | .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) |
435 | 447 | .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) |
436 | 448 | .setContentIntent(statusConfig.getClickIntent(service)) |
@@ -462,7 +474,7 @@ private void updateNotificationProgress(UploadInfo uploadInfo) { |
462 | 474 |
|
463 | 475 | UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); |
464 | 476 |
|
465 | | - NotificationCompat.Builder notification = new NotificationCompat.Builder(service) |
| 477 | + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) |
466 | 478 | .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) |
467 | 479 | .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) |
468 | 480 | .setContentIntent(statusConfig.getClickIntent(service)) |
@@ -501,7 +513,7 @@ private void updateNotification(UploadInfo uploadInfo, UploadNotificationStatusC |
501 | 513 | if (statusConfig.message == null) return; |
502 | 514 |
|
503 | 515 | if (!statusConfig.autoClear) { |
504 | | - NotificationCompat.Builder notification = new NotificationCompat.Builder(service) |
| 516 | + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) |
505 | 517 | .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) |
506 | 518 | .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) |
507 | 519 | .setContentIntent(statusConfig.getClickIntent(service)) |
|
0 commit comments