Refactor creation of Notifications channel

This commit is contained in:
Amab 2022-12-18 08:33:35 +01:00
parent 50591b17b6
commit e7e3d7fa9d
2 changed files with 18 additions and 10 deletions

View File

@ -57,6 +57,7 @@ import java.util.List;
import java.util.Map;
import es.ugr.swad.swadroid.database.DataBaseHelper;
import es.ugr.swad.swadroid.gui.AlertNotificationFactory;
import es.ugr.swad.swadroid.gui.DialogFactory;
import es.ugr.swad.swadroid.gui.MenuExpandableListActivity;
import es.ugr.swad.swadroid.gui.ProgressScreen;
@ -173,6 +174,11 @@ public class SWADMain extends MenuExpandableListActivity {
initializeMainViews();
try {
// Create Notifications channel
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
AlertNotificationFactory.createNotificationChanel(this);
}
//Check if this is the first run after an install or upgrade
lastVersion = Preferences.getLastVersion();
currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

View File

@ -29,6 +29,7 @@ import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import es.ugr.swad.swadroid.utils.NotificationUtils;
@ -134,16 +135,6 @@ public class AlertNotificationFactory {
}
public static Notification createBackgroundNotification(Context context, String contentTitle, int smallIcon, int largeIcon) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String channelName = "Background Service";
NotificationChannel channel = new NotificationChannel(SWADROID_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
channel.setLightColor(Color.BLUE);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, SWADROID_CHANNEL_ID)
.setOngoing(true)
.setSmallIcon(smallIcon)
@ -156,6 +147,17 @@ public class AlertNotificationFactory {
return notifBuilder.build();
}
@RequiresApi(android.os.Build.VERSION_CODES.O)
public static void createNotificationChanel(Context context) {
String channelName = "Background Service";
NotificationChannel channel = new NotificationChannel(SWADROID_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
channel.setLightColor(Color.BLUE);
manager.createNotificationChannel(channel);
}
public static void showAlertNotification(Context context, Notification notif, int notifId) {
NotificationManager notifManager;