From 957a8f051967eeb40bc400048328ba1db21e5222 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 31 Jul 2017 15:48:43 +0200 Subject: [PATCH 1/5] Android Support Library updated to latest Added support of NotificationChannels, with possibility to override him through protected createNotificationChannel of UploadTask class --- build.gradle | 1 + uploadservice-ftp/build.gradle | 6 +++--- uploadservice-okhttp/build.gradle | 6 +++--- uploadservice/build.gradle | 8 ++++---- .../UploadNotificationConfig.java | 8 ++++---- .../UploadNotificationStatusConfig.java | 11 ++++++++++- .../net/gotev/uploadservice/UploadTask.java | 19 ++++++++++++++++--- 7 files changed, 41 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 2a93cd9d..d831ee75 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ buildscript { allprojects { repositories { jcenter() + maven { url 'http://maven.google.com' } mavenCentral() } } diff --git a/uploadservice-ftp/build.gradle b/uploadservice-ftp/build.gradle index 1c37b869..b61fe917 100644 --- a/uploadservice-ftp/build.gradle +++ b/uploadservice-ftp/build.gradle @@ -10,14 +10,14 @@ def projectGroup = "net.gotev" group = projectGroup version = "3.3" -def sdkVersion = 25; +def sdkVersion = 26; android { compileSdkVersion sdkVersion - buildToolsVersion '25.0.2' + buildToolsVersion '26.0.1' defaultConfig { - minSdkVersion 10 + minSdkVersion 14 targetSdkVersion sdkVersion versionCode 13 versionName version diff --git a/uploadservice-okhttp/build.gradle b/uploadservice-okhttp/build.gradle index fdfa8c7b..2a08a585 100644 --- a/uploadservice-okhttp/build.gradle +++ b/uploadservice-okhttp/build.gradle @@ -10,14 +10,14 @@ def projectGroup = "net.gotev" group = projectGroup version = "3.3" -def sdkVersion = 25; +def sdkVersion = 26; android { compileSdkVersion sdkVersion - buildToolsVersion '25.0.2' + buildToolsVersion '26.0.1' defaultConfig { - minSdkVersion 10 + minSdkVersion 14 targetSdkVersion sdkVersion versionCode 10 versionName version diff --git a/uploadservice/build.gradle b/uploadservice/build.gradle index 967a1077..855d44d6 100644 --- a/uploadservice/build.gradle +++ b/uploadservice/build.gradle @@ -10,14 +10,14 @@ def projectGroup = "net.gotev" group = projectGroup version = "3.3" -def sdkVersion = 25; +def sdkVersion = 26; android { compileSdkVersion sdkVersion - buildToolsVersion '25.0.2' + buildToolsVersion '26.0.1' defaultConfig { - minSdkVersion 10 + minSdkVersion 14 targetSdkVersion sdkVersion versionCode 26 versionName version @@ -33,7 +33,7 @@ android { } } -def supportLibraryVersion = "25.3.1" +def supportLibraryVersion = "26.0.0" dependencies { compile "com.android.support:appcompat-v7:${supportLibraryVersion}" diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java index c2bc7925..f6d6d456 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java @@ -38,19 +38,19 @@ public UploadNotificationConfig() { ringToneEnabled = true; // progress notification configuration - progress = new UploadNotificationStatusConfig(); + progress = new UploadNotificationStatusConfig("net.gotev.uploadservice"); progress.message = "Uploading at " + Placeholders.UPLOAD_RATE + " (" + Placeholders.PROGRESS + ")"; // completed notification configuration - completed = new UploadNotificationStatusConfig(); + completed = new UploadNotificationStatusConfig("net.gotev.uploadservice"); completed.message = "Upload completed successfully in " + Placeholders.ELAPSED_TIME; // error notification configuration - error = new UploadNotificationStatusConfig(); + error = new UploadNotificationStatusConfig("net.gotev.uploadservice"); error.message = "Error during upload"; // cancelled notification configuration - cancelled = new UploadNotificationStatusConfig(); + cancelled = new UploadNotificationStatusConfig("net.gotev.uploadservice"); cancelled.message = "Upload cancelled"; } diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java index 1404bba0..377903e7 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java @@ -6,6 +6,7 @@ import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import java.util.ArrayList; @@ -16,6 +17,11 @@ public class UploadNotificationStatusConfig implements Parcelable { + /** + * Notification channel + */ + @NonNull + public String channel; /** * Notification title. */ @@ -87,6 +93,7 @@ public int describeContents() { @Override public void writeToParcel(Parcel dest, int flags) { + dest.writeString(this.channel); dest.writeString(this.title); dest.writeString(this.message); dest.writeByte(this.autoClear ? (byte) 1 : (byte) 0); @@ -98,10 +105,12 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeTypedList(this.actions); } - public UploadNotificationStatusConfig() { + public UploadNotificationStatusConfig(@NonNull String notificationChannel){ + this.channel = notificationChannel; } protected UploadNotificationStatusConfig(Parcel in) { + this.channel = in.readString(); this.title = in.readString(); this.message = in.readString(); this.autoClear = in.readByte() != 0; diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java index 97e57bac..35e56f66 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java @@ -1,11 +1,15 @@ package net.gotev.uploadservice; +import android.annotation.TargetApi; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; +import android.os.Build; import android.os.Handler; +import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import java.io.File; @@ -115,11 +119,20 @@ public UploadTask() { */ protected void init(UploadService service, Intent intent) throws IOException { this.notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = createNotificationChannel("net.gotev.uploadservice"); + this.notificationManager.createNotificationChannel(channel); + } this.service = service; this.mainThreadHandler = new Handler(service.getMainLooper()); this.params = intent.getParcelableExtra(UploadService.PARAM_TASK_PARAMETERS); } + @TargetApi(Build.VERSION_CODES.O) + protected NotificationChannel createNotificationChannel(@NonNull String channelId) { + return new NotificationChannel(channelId, "Upload Service channel", NotificationManager.IMPORTANCE_DEFAULT); + } + @Override public final void run() { @@ -430,7 +443,7 @@ private void createNotification(UploadInfo uploadInfo) { UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); - NotificationCompat.Builder notification = new NotificationCompat.Builder(service) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.channel) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) @@ -462,7 +475,7 @@ private void updateNotificationProgress(UploadInfo uploadInfo) { UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); - NotificationCompat.Builder notification = new NotificationCompat.Builder(service) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.channel) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) @@ -501,7 +514,7 @@ private void updateNotification(UploadInfo uploadInfo, UploadNotificationStatusC if (statusConfig.message == null) return; if (!statusConfig.autoClear) { - NotificationCompat.Builder notification = new NotificationCompat.Builder(service) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.channel) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) From 8f60e07a26b1405cad5c60982606a673fe2f6bc3 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 1 Aug 2017 10:40:15 +0200 Subject: [PATCH 2/5] Default notificationChannel uses UploadService.NAMESPACE now Added method setNotificationChannelForAllStatuses --- .../UploadNotificationConfig.java | 22 +++++++++++++++---- .../UploadNotificationStatusConfig.java | 10 ++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java index f6d6d456..f5feb931 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java @@ -4,6 +4,7 @@ import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.NonNull; /** * Contains the configuration of the upload notification. @@ -38,19 +39,19 @@ public UploadNotificationConfig() { ringToneEnabled = true; // progress notification configuration - progress = new UploadNotificationStatusConfig("net.gotev.uploadservice"); + progress = new UploadNotificationStatusConfig(); progress.message = "Uploading at " + Placeholders.UPLOAD_RATE + " (" + Placeholders.PROGRESS + ")"; // completed notification configuration - completed = new UploadNotificationStatusConfig("net.gotev.uploadservice"); + completed = new UploadNotificationStatusConfig(); completed.message = "Upload completed successfully in " + Placeholders.ELAPSED_TIME; // error notification configuration - error = new UploadNotificationStatusConfig("net.gotev.uploadservice"); + error = new UploadNotificationStatusConfig(); error.message = "Error during upload"; // cancelled notification configuration - cancelled = new UploadNotificationStatusConfig("net.gotev.uploadservice"); + cancelled = new UploadNotificationStatusConfig(); cancelled.message = "Upload cancelled"; } @@ -93,6 +94,19 @@ public final UploadNotificationConfig setIconColorForAllStatuses(int iconColorRe return this; } + /** + * Sets the same notification channel ID for all the notification statuses. + * @param notificationChannel Notification channel ID + * @return {@link UploadNotificationConfig} + */ + public final UploadNotificationConfig setNotificationChannelForAllStatuses(@NonNull String notificationChannel) { + progress.notificationChannel = notificationChannel; + completed.notificationChannel = notificationChannel; + error.notificationChannel = notificationChannel; + cancelled.notificationChannel = notificationChannel; + return this; + } + /** * Sets the same large notification icon for all the notification statuses. * @param largeIcon Bitmap of the icon to use diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java index 377903e7..d44de5f7 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java @@ -21,7 +21,7 @@ public class UploadNotificationStatusConfig implements Parcelable { * Notification channel */ @NonNull - public String channel; + public String notificationChannel; /** * Notification title. */ @@ -93,7 +93,7 @@ public int describeContents() { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeString(this.channel); + dest.writeString(this.notificationChannel); dest.writeString(this.title); dest.writeString(this.message); dest.writeByte(this.autoClear ? (byte) 1 : (byte) 0); @@ -105,12 +105,12 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeTypedList(this.actions); } - public UploadNotificationStatusConfig(@NonNull String notificationChannel){ - this.channel = notificationChannel; + public UploadNotificationStatusConfig(){ + this.notificationChannel = UploadService.NAMESPACE; } protected UploadNotificationStatusConfig(Parcel in) { - this.channel = in.readString(); + this.notificationChannel = in.readString(); this.title = in.readString(); this.message = in.readString(); this.autoClear = in.readByte() != 0; From f3af569881cb0ae3396cdef07ab49eec3570ecdd Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 1 Aug 2017 13:19:22 +0200 Subject: [PATCH 3/5] Build fixes --- .../uploadservice/UploadNotificationStatusConfig.java | 10 +++++----- .../main/java/net/gotev/uploadservice/UploadTask.java | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java index d44de5f7..10742644 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java @@ -18,10 +18,10 @@ public class UploadNotificationStatusConfig implements Parcelable { /** - * Notification channel + * Notification channel ID */ @NonNull - public String notificationChannel; + public String notificationChannelId; /** * Notification title. */ @@ -93,7 +93,7 @@ public int describeContents() { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeString(this.notificationChannel); + dest.writeString(this.notificationChannelId); dest.writeString(this.title); dest.writeString(this.message); dest.writeByte(this.autoClear ? (byte) 1 : (byte) 0); @@ -106,11 +106,11 @@ public void writeToParcel(Parcel dest, int flags) { } public UploadNotificationStatusConfig(){ - this.notificationChannel = UploadService.NAMESPACE; + this.notificationChannelId = UploadService.NAMESPACE; } protected UploadNotificationStatusConfig(Parcel in) { - this.notificationChannel = in.readString(); + this.notificationChannelId = in.readString(); this.title = in.readString(); this.message = in.readString(); this.autoClear = in.readByte() != 0; diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java index 35e56f66..9c20fb68 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java @@ -120,7 +120,7 @@ public UploadTask() { protected void init(UploadService service, Intent intent) throws IOException { this.notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = createNotificationChannel("net.gotev.uploadservice"); + NotificationChannel channel = createNotificationChannel(UploadService.NAMESPACE); this.notificationManager.createNotificationChannel(channel); } this.service = service; @@ -443,7 +443,7 @@ private void createNotification(UploadInfo uploadInfo) { UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); - NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.channel) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.notificationChannelId) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) @@ -475,7 +475,7 @@ private void updateNotificationProgress(UploadInfo uploadInfo) { UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); - NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.channel) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.notificationChannelId) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) @@ -514,7 +514,7 @@ private void updateNotification(UploadInfo uploadInfo, UploadNotificationStatusC if (statusConfig.message == null) return; if (!statusConfig.autoClear) { - NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.channel) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.notificationChannelId) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) From bdf074ab45c2f0773a8f5ece16965da1386a511e Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 1 Aug 2017 13:22:05 +0200 Subject: [PATCH 4/5] Build fixes --- .../uploadservice/UploadNotificationConfig.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java index f5feb931..c0533a8c 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java @@ -96,14 +96,14 @@ public final UploadNotificationConfig setIconColorForAllStatuses(int iconColorRe /** * Sets the same notification channel ID for all the notification statuses. - * @param notificationChannel Notification channel ID + * @param notificationChannelId Notification channel ID * @return {@link UploadNotificationConfig} */ - public final UploadNotificationConfig setNotificationChannelForAllStatuses(@NonNull String notificationChannel) { - progress.notificationChannel = notificationChannel; - completed.notificationChannel = notificationChannel; - error.notificationChannel = notificationChannel; - cancelled.notificationChannel = notificationChannel; + public final UploadNotificationConfig setNotificationChannelForAllStatuses(@NonNull String notificationChannelId) { + progress.notificationChannelId = notificationChannelId; + completed.notificationChannelId = notificationChannelId; + error.notificationChannelId = notificationChannelId; + cancelled.notificationChannelId = notificationChannelId; return this; } From c1fd9f7509eaeee0ea324418ee76199f69220bdc Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 2 Aug 2017 10:20:57 +0200 Subject: [PATCH 5/5] Notification channel creation logic updated TravisCI fixed --- .travis.yml | 4 +-- .../UploadNotificationConfig.java | 35 ++++++++++++------- .../UploadNotificationStatusConfig.java | 9 ----- .../net/gotev/uploadservice/UploadTask.java | 23 ++++++------ 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/.travis.yml b/.travis.yml index c88a0954..c715e9f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,10 +11,10 @@ android: - platform-tools # The BuildTools version used by your project - - build-tools-25.0.2 + - build-tools-26.0.1 # The SDK version used to compile your project - - android-25 + - android-26 # Additional components - extra-google-m2repository diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java index c0533a8c..c6e4b68b 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationConfig.java @@ -15,6 +15,11 @@ public final class UploadNotificationConfig implements Parcelable { private boolean ringToneEnabled; + /** + * Notification channel ID + */ + private String notificationChannelId; + private UploadNotificationStatusConfig progress; private UploadNotificationStatusConfig completed; private UploadNotificationStatusConfig error; @@ -94,19 +99,6 @@ public final UploadNotificationConfig setIconColorForAllStatuses(int iconColorRe return this; } - /** - * Sets the same notification channel ID for all the notification statuses. - * @param notificationChannelId Notification channel ID - * @return {@link UploadNotificationConfig} - */ - public final UploadNotificationConfig setNotificationChannelForAllStatuses(@NonNull String notificationChannelId) { - progress.notificationChannelId = notificationChannelId; - completed.notificationChannelId = notificationChannelId; - error.notificationChannelId = notificationChannelId; - cancelled.notificationChannelId = notificationChannelId; - return this; - } - /** * Sets the same large notification icon for all the notification statuses. * @param largeIcon Bitmap of the icon to use @@ -181,6 +173,17 @@ public final UploadNotificationConfig setRingToneEnabled(Boolean enabled) { return this; } + /** + * Sets notification channel ID + * + * @param channelId notification channel ID + * @return {@link UploadNotificationConfig} + */ + public final UploadNotificationConfig setNotificationChannelId(@NonNull String channelId){ + this.notificationChannelId = channelId; + return this; + } + public boolean isRingToneEnabled() { return ringToneEnabled; } @@ -201,6 +204,10 @@ public UploadNotificationStatusConfig getCancelled() { return cancelled; } + public String getNotificationChannelId(){ + return notificationChannelId; + } + @Override public int describeContents() { return 0; @@ -208,6 +215,7 @@ public int describeContents() { @Override public void writeToParcel(Parcel dest, int flags) { + dest.writeString(this.notificationChannelId); dest.writeByte(this.ringToneEnabled ? (byte) 1 : (byte) 0); dest.writeParcelable(this.progress, flags); dest.writeParcelable(this.completed, flags); @@ -216,6 +224,7 @@ public void writeToParcel(Parcel dest, int flags) { } protected UploadNotificationConfig(Parcel in) { + this.notificationChannelId = in.readString(); this.ringToneEnabled = in.readByte() != 0; this.progress = in.readParcelable(UploadNotificationStatusConfig.class.getClassLoader()); this.completed = in.readParcelable(UploadNotificationStatusConfig.class.getClassLoader()); diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java index 10742644..d81d2bed 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadNotificationStatusConfig.java @@ -6,7 +6,6 @@ import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; -import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import java.util.ArrayList; @@ -17,11 +16,6 @@ public class UploadNotificationStatusConfig implements Parcelable { - /** - * Notification channel ID - */ - @NonNull - public String notificationChannelId; /** * Notification title. */ @@ -93,7 +87,6 @@ public int describeContents() { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeString(this.notificationChannelId); dest.writeString(this.title); dest.writeString(this.message); dest.writeByte(this.autoClear ? (byte) 1 : (byte) 0); @@ -106,11 +99,9 @@ public void writeToParcel(Parcel dest, int flags) { } public UploadNotificationStatusConfig(){ - this.notificationChannelId = UploadService.NAMESPACE; } protected UploadNotificationStatusConfig(Parcel in) { - this.notificationChannelId = in.readString(); this.title = in.readString(); this.message = in.readString(); this.autoClear = in.readByte() != 0; diff --git a/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java b/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java index 9c20fb68..b12034a4 100644 --- a/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java +++ b/uploadservice/src/main/java/net/gotev/uploadservice/UploadTask.java @@ -1,6 +1,5 @@ package net.gotev.uploadservice; -import android.annotation.TargetApi; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; @@ -9,7 +8,6 @@ import android.media.RingtoneManager; import android.os.Build; import android.os.Handler; -import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import java.io.File; @@ -120,19 +118,20 @@ public UploadTask() { protected void init(UploadService service, Intent intent) throws IOException { this.notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = createNotificationChannel(UploadService.NAMESPACE); - this.notificationManager.createNotificationChannel(channel); + String notificationChannelId = params.notificationConfig.getNotificationChannelId(); + if (notificationChannelId == null) { + notificationChannelId = UploadService.NAMESPACE; + } + if (notificationManager.getNotificationChannel(notificationChannelId) == null) { + NotificationChannel channel = new NotificationChannel(notificationChannelId, "Upload Service channel", NotificationManager.IMPORTANCE_DEFAULT); + notificationManager.createNotificationChannel(channel); + } } this.service = service; this.mainThreadHandler = new Handler(service.getMainLooper()); this.params = intent.getParcelableExtra(UploadService.PARAM_TASK_PARAMETERS); } - @TargetApi(Build.VERSION_CODES.O) - protected NotificationChannel createNotificationChannel(@NonNull String channelId) { - return new NotificationChannel(channelId, "Upload Service channel", NotificationManager.IMPORTANCE_DEFAULT); - } - @Override public final void run() { @@ -443,7 +442,7 @@ private void createNotification(UploadInfo uploadInfo) { UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); - NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.notificationChannelId) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) @@ -475,7 +474,7 @@ private void updateNotificationProgress(UploadInfo uploadInfo) { UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); - NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.notificationChannelId) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) @@ -514,7 +513,7 @@ private void updateNotification(UploadInfo uploadInfo, UploadNotificationStatusC if (statusConfig.message == null) return; if (!statusConfig.autoClear) { - NotificationCompat.Builder notification = new NotificationCompat.Builder(service, statusConfig.notificationChannelId) + NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service))