Add ugly hacky oreo notification support
This commit is contained in:
@@ -108,6 +108,8 @@ public class PushService extends Service {
|
|||||||
handler = new Handler();
|
handler = new Handler();
|
||||||
new Thread(pushService).start();
|
new Thread(pushService).start();
|
||||||
appPreferences().registerOnSharedPreferenceChangeListener(listener);
|
appPreferences().registerOnSharedPreferenceChangeListener(listener);
|
||||||
|
|
||||||
|
UglyHackyOreoNotificationCompatibilitySupport.uglyInitializeNotificationChannel((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void foregroundNotification(String message) {
|
private void foregroundNotification(String message) {
|
||||||
@@ -118,6 +120,7 @@ public class PushService extends Service {
|
|||||||
Notification notification = new NotificationCompat.Builder(this, "GOTIFY_CHANNEL")
|
Notification notification = new NotificationCompat.Builder(this, "GOTIFY_CHANNEL")
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
.setContentTitle("Gotify")
|
.setContentTitle("Gotify")
|
||||||
|
.setChannelId(UglyHackyOreoNotificationCompatibilitySupport.CHANNEL_ID)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setPriority(Notification.PRIORITY_MIN)
|
.setPriority(Notification.PRIORITY_MIN)
|
||||||
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
|
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
|
||||||
@@ -286,6 +289,7 @@ public class PushService extends Service {
|
|||||||
.setContentText(message)
|
.setContentText(message)
|
||||||
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
|
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
|
||||||
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
|
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
|
||||||
|
.setChannelId(UglyHackyOreoNotificationCompatibilitySupport.CHANNEL_ID)
|
||||||
.setContentIntent(contentIntent);
|
.setContentIntent(contentIntent);
|
||||||
|
|
||||||
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
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 <a href="https://stackoverflow.com/a/48861133/4244993">Code by Elad Nava</a>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user