From e88409ccba2d3c12a31fb596af81303080d404b9 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 23 Jun 2018 10:30:54 +0200 Subject: [PATCH] Fix ugly hacky oreo notification support --- .../de/gotify/OreoNotificationSupport.java | 24 ++++++++++ .../src/main/java/de/gotify/PushService.java | 9 ++-- ...yOreoNotificationCompatibilitySupport.java | 48 ------------------- 3 files changed, 30 insertions(+), 51 deletions(-) create mode 100644 android/app/src/main/java/de/gotify/OreoNotificationSupport.java delete mode 100644 android/app/src/main/java/de/gotify/UglyHackyOreoNotificationCompatibilitySupport.java diff --git a/android/app/src/main/java/de/gotify/OreoNotificationSupport.java b/android/app/src/main/java/de/gotify/OreoNotificationSupport.java new file mode 100644 index 0000000..43c684b --- /dev/null +++ b/android/app/src/main/java/de/gotify/OreoNotificationSupport.java @@ -0,0 +1,24 @@ +package de.gotify; + + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.os.Build; +import android.support.annotation.RequiresApi; + +/** + * Creates a Notification channel for android oreo. + */ +public class OreoNotificationSupport { + public static final String CHANNEL_ID = "gotify"; + + @RequiresApi(Build.VERSION_CODES.O) + public static void createChannel(NotificationManager notificationManager) { + try { + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Gotify", NotificationManager.IMPORTANCE_DEFAULT); + notificationManager.createNotificationChannel(channel); + } catch (Exception e) { + Log.e("Could not create channel", e); + } + } +} \ No newline at end of file diff --git a/android/app/src/main/java/de/gotify/PushService.java b/android/app/src/main/java/de/gotify/PushService.java index ec4a843..3999fd1 100644 --- a/android/app/src/main/java/de/gotify/PushService.java +++ b/android/app/src/main/java/de/gotify/PushService.java @@ -7,6 +7,7 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.support.v4.app.NotificationCompat; @@ -109,7 +110,9 @@ public class PushService extends Service { new Thread(pushService).start(); appPreferences().registerOnSharedPreferenceChangeListener(listener); - UglyHackyOreoNotificationCompatibilitySupport.uglyInitializeNotificationChannel((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + OreoNotificationSupport.createChannel((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE)); + } } private void foregroundNotification(String message) { @@ -120,7 +123,7 @@ public class PushService extends Service { Notification notification = new NotificationCompat.Builder(this, "GOTIFY_CHANNEL") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Gotify") - .setChannelId(UglyHackyOreoNotificationCompatibilitySupport.CHANNEL_ID) + .setChannelId(OreoNotificationSupport.CHANNEL_ID) .setOngoing(true) .setPriority(Notification.PRIORITY_MIN) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) @@ -289,7 +292,7 @@ public class PushService extends Service { .setContentText(message) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) - .setChannelId(UglyHackyOreoNotificationCompatibilitySupport.CHANNEL_ID) + .setChannelId(OreoNotificationSupport.CHANNEL_ID) .setContentIntent(contentIntent); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); diff --git a/android/app/src/main/java/de/gotify/UglyHackyOreoNotificationCompatibilitySupport.java b/android/app/src/main/java/de/gotify/UglyHackyOreoNotificationCompatibilitySupport.java deleted file mode 100644 index 5edb3ca..0000000 --- a/android/app/src/main/java/de/gotify/UglyHackyOreoNotificationCompatibilitySupport.java +++ /dev/null @@ -1,48 +0,0 @@ -package de.gotify; - - -import android.app.NotificationManager; -import android.os.Build; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; - -/** - * I hate reflections and android. - */ -public class UglyHackyOreoNotificationCompatibilitySupport { - public static final String CHANNEL_ID = "gotify"; - - /** - * @param notificationManager the notification manager - * @see Code by Elad Nava - */ - public static void uglyInitializeNotificationChannel(NotificationManager notificationManager) { - if (Build.VERSION.SDK_INT < 26) { - return; - } - - // Channel importance (3 means default importance) - int channelImportance = 3; - - try { - // Get NotificationChannel class via reflection (only available on devices running Android O or newer) - Class notificationChannelClass = Class.forName("android.app.NotificationChannel"); - - // Get NotificationChannel constructor - Constructor notificationChannelConstructor = notificationChannelClass.getDeclaredConstructor(String.class, CharSequence.class, int.class); - - // Instantiate new notification channel - Object notificationChannel = notificationChannelConstructor.newInstance(CHANNEL_ID, "Gotify", channelImportance); - - // Get notification channel creation method via reflection - Method createNotificationChannelMethod = notificationManager.getClass().getDeclaredMethod("createNotificationChannel", notificationChannelClass); - - // Invoke method on NotificationManager, passing in the channel object - createNotificationChannelMethod.invoke(notificationManager, notificationChannel); - - } catch (Exception e) { - Log.e("Creating notification channel failed.", e); - } - } -} \ No newline at end of file