[Android] Create Notification channel at App Launch

Geoffrey Métais git at videolan.org
Wed Aug 23 18:17:43 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Aug 23 17:54:09 2017 +0200| [17e00cf2b845df05af619dfb988ec5f6a44f40a5] | committer: Geoffrey Métais

Create Notification channel at App Launch

> https://code.videolan.org/videolan/vlc-android/commit/17e00cf2b845df05af619dfb988ec5f6a44f40a5
---

 vlc-android/src/org/videolan/vlc/VLCApplication.java   |  3 +++
 .../videolan/vlc/gui/helpers/NotificationHelper.java   | 18 +++++++-----------
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index 664971658..0356eac24 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -40,6 +40,7 @@ import org.videolan.vlc.gui.DialogActivity;
 import org.videolan.vlc.gui.dialogs.VlcProgressDialog;
 import org.videolan.vlc.gui.helpers.AudioUtil;
 import org.videolan.vlc.gui.helpers.BitmapCache;
+import org.videolan.vlc.gui.helpers.NotificationHelper;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.VLCInstance;
@@ -94,6 +95,8 @@ public class VLCApplication extends Application {
         runBackground(new Runnable() {
             @Override
             public void run() {
+                if (AndroidUtil.isOOrLater)
+                    NotificationHelper.createNotificationChannel();
                 // Prepare cache folder constants
                 AudioUtil.prepareCacheFolder(instance);
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
index 26bc72a04..1de8e997e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.java
@@ -58,7 +58,6 @@ public class NotificationHelper {
         final PendingIntent piPlay = PendingIntent.getBroadcast(ctx, 0, new Intent(PlaybackService.ACTION_REMOTE_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
         final PendingIntent piForward = PendingIntent.getBroadcast(ctx, 0, new Intent(PlaybackService.ACTION_REMOTE_FORWARD), PendingIntent.FLAG_UPDATE_CURRENT);
         if (AndroidUtil.isOOrLater) {
-            createNotificationChannel();
             final Notification.Builder builder = new Notification.Builder(ctx, "vlc_channel");
             builder.setSmallIcon(video ? R.drawable.ic_notif_video : R.drawable.ic_notif_audio)
                     .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
@@ -121,7 +120,6 @@ public class NotificationHelper {
     private static final Intent notificationIntent = new Intent();
     public static Notification createScanNotification(Context ctx, String progressText, boolean updateActions, boolean paused) {
         if (AndroidUtil.isOOrLater) {
-            createNotificationChannel();
             if (scanBuilder == null) {
                 scanBuilder = new Notification.Builder(ctx, "vlc_channel")
                         .setContentIntent(PendingIntent.getActivity(ctx, 0, new Intent(ctx, StartActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
@@ -170,16 +168,14 @@ public class NotificationHelper {
 
     private static NotificationManager sNotificationManager;
     @RequiresApi(api = Build.VERSION_CODES.O)
-    private static void createNotificationChannel() {
+    public static void createNotificationChannel() {
         if (sNotificationManager == null)
             sNotificationManager = (NotificationManager) VLCApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
-        if (sNotificationManager.getNotificationChannel("vlc_channel") == null) {
-            final CharSequence name = VLCApplication.getAppResources().getString(R.string.app_name);
-            final String description = VLCApplication.getAppResources().getString(R.string.app_name_full);
-            final int importance = NotificationManager.IMPORTANCE_DEFAULT;
-            NotificationChannel mChannel = new NotificationChannel("vlc_channel", name, importance);
-            mChannel.setDescription(description);
-            sNotificationManager.createNotificationChannel(mChannel);
-        }
+        final CharSequence name = VLCApplication.getAppResources().getString(R.string.app_name);
+        final String description = VLCApplication.getAppResources().getString(R.string.app_name_full);
+        final int importance = NotificationManager.IMPORTANCE_DEFAULT;
+        NotificationChannel mChannel = new NotificationChannel("vlc_channel", name, importance);
+        mChannel.setDescription(description);
+        sNotificationManager.createNotificationChannel(mChannel);
     }
 }



More information about the Android mailing list