From e0a79f89e9a5b6b0046d05e405d056cb3f884845 Mon Sep 17 00:00:00 2001 From: Dmitry Brant Date: Thu, 27 Sep 2018 11:39:32 -0400 Subject: [PATCH] Fix crash(es) due to uninitialized notification channel. (#1906) * Fix crash(es) due to uninitialized notification channel. The notification channel needs to be created for API versions greater than OR EQUAL to 26 (O). Also, the channel does not need to be reinitialized if it already exists. * Initialize notification channel when Service is created. --- .../free/nrw/commons/CommonsApplication.java | 23 ++++++++++--------- .../nrw/commons/upload/UploadService.java | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java b/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java index 09b39c6e1..c4ff5ad61 100644 --- a/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java +++ b/app/src/main/java/fr/free/nrw/commons/CommonsApplication.java @@ -8,6 +8,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Build; +import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import com.facebook.drawee.backends.pipeline.Fresco; @@ -122,22 +123,22 @@ public class CommonsApplication extends Application { Stetho.initializeWithDefaults(this); } - - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { - createNotificationChannel(); - } + createNotificationChannel(this); // Fire progress callbacks for every 3% of uploaded content System.setProperty("in.yuvi.http.fluent.PROGRESS_TRIGGER_THRESHOLD", "3.0"); } - @RequiresApi(26) - private void createNotificationChannel() { - NotificationChannel channel = new NotificationChannel( - NOTIFICATION_CHANNEL_ID_ALL, - getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_NONE); - NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - manager.createNotificationChannel(channel); + public static void createNotificationChannel(@NonNull Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationChannel channel = manager.getNotificationChannel(NOTIFICATION_CHANNEL_ID_ALL); + if (channel == null) { + channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_ALL, + context.getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_DEFAULT); + manager.createNotificationChannel(channel); + } + } } /** diff --git a/app/src/main/java/fr/free/nrw/commons/upload/UploadService.java b/app/src/main/java/fr/free/nrw/commons/upload/UploadService.java index c2fdfbe8d..a5b8da456 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/UploadService.java +++ b/app/src/main/java/fr/free/nrw/commons/upload/UploadService.java @@ -121,7 +121,7 @@ public class UploadService extends HandlerService { @Override public void onCreate() { super.onCreate(); - + CommonsApplication.createNotificationChannel(getApplicationContext()); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); }