Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/backend/Notifications.java
| package it.reyboz.bustorino.backend; | package it.reyboz.bustorino.backend; | ||||
| import android.app.Notification; | import android.app.Notification; | ||||
| import android.app.NotificationChannel; | import android.app.NotificationChannel; | ||||
| import android.app.NotificationManager; | import android.app.NotificationManager; | ||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.os.Build; | import android.os.Build; | ||||
| import androidx.core.app.NotificationCompat; | import androidx.core.app.NotificationCompat; | ||||
| import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
| public class Notifications { | public class Notifications { | ||||
| public static final String DEFAULT_CHANNEL_ID ="Default"; | public static final String DEFAULT_CHANNEL_ID ="Default"; | ||||
| public static final String DB_UPDATE_CHANNELS_ID ="Database Update"; | public static final String DB_UPDATE_CHANNELS_ID ="Database Update"; | ||||
| public static final String MATO_LIVE_POSITIONS_CHANNEL="Live Positions"; | |||||
| //match this value to the one used by the MQTTAndroidClient MANUALLY | //match this value to the one used by the MQTTAndroidClient MANUALLY | ||||
| public static void createDefaultNotificationChannel(Context context) { | public static void createDefaultNotificationChannel(Context context) { | ||||
| // Create the NotificationChannel, but only on API 26+ because | // Create the NotificationChannel, but only on API 26+ because | ||||
| // the NotificationChannel class is new and not in the support library | // the NotificationChannel class is new and not in the support library | ||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||||
| CharSequence name = context.getString(R.string.default_notification_channel); | CharSequence name = context.getString(R.string.default_notification_channel); | ||||
| Show All 37 Lines | public static Notification makeMatoDownloadNotification(Context context,String title){ | ||||
| .setOnlyAlertOnce(true) | .setOnlyAlertOnce(true) | ||||
| .setPriority(NotificationCompat.PRIORITY_MIN) | .setPriority(NotificationCompat.PRIORITY_MIN) | ||||
| .setContentTitle(context.getString(R.string.app_name)) | .setContentTitle(context.getString(R.string.app_name)) | ||||
| .setLocalOnly(true) | .setLocalOnly(true) | ||||
| .setVisibility(NotificationCompat.VISIBILITY_SECRET) | .setVisibility(NotificationCompat.VISIBILITY_SECRET) | ||||
| .setContentText(title) | .setContentText(title) | ||||
| .build(); | .build(); | ||||
| } | } | ||||
| public static Notification makeLivePositionsNotification(Context context,String title){ | |||||
| return new NotificationCompat.Builder(context, Notifications.MATO_LIVE_POSITIONS_CHANNEL) | |||||
| //.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), Constants.PENDING_INTENT_FLAG_IMMUTABLE)) | |||||
| .setSmallIcon(R.drawable.ic_bus_stilized_transparent) | |||||
| .setOngoing(true) | |||||
| .setAutoCancel(true) | |||||
| .setOnlyAlertOnce(true) | |||||
| .setPriority(NotificationCompat.PRIORITY_MIN) | |||||
| .setContentTitle(context.getString(R.string.app_name)) | |||||
| .setLocalOnly(true) | |||||
| .setVisibility(NotificationCompat.VISIBILITY_SECRET) | |||||
| .setContentText(title) | |||||
| .build(); | |||||
| } | |||||
| public static Notification makeMatoDownloadNotification(Context context){ | public static Notification makeMatoDownloadNotification(Context context){ | ||||
| return makeMatoDownloadNotification(context, context.getString(R.string.downloading_data_mato)); | return makeMatoDownloadNotification(context, context.getString(R.string.downloading_data_mato)); | ||||
| } | } | ||||
| public static Notification makeMQTTServiceNotification(Context context){ | public static Notification makeMQTTServiceNotification(Context context){ | ||||
| return makeMatoDownloadNotification(context, context.getString(R.string.mqtt_notification_text)); | return makeLivePositionsNotification(context, context.getString(R.string.mqtt_notification_text)); | ||||
| } | } | ||||
| public static void cancelNotification(Context context, int notificationID){ | public static void cancelNotification(Context context, int notificationID){ | ||||
| NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||||
| manager.cancel(notificationID); | manager.cancel(notificationID); | ||||
| } | } | ||||
| public static void createDBNotificationChannel(Context context){ | public static void createDBNotificationChannel(Context context){ | ||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||||
| NotificationChannel channel = new NotificationChannel( | NotificationChannel channel = new NotificationChannel( | ||||
| Notifications.DB_UPDATE_CHANNELS_ID, | Notifications.DB_UPDATE_CHANNELS_ID, | ||||
| context.getString(R.string.database_notification_channel), | context.getString(R.string.database_notification_channel), | ||||
| NotificationManager.IMPORTANCE_MIN | NotificationManager.IMPORTANCE_MIN | ||||
| ); | ); | ||||
| NotificationManager notificationManager = context.getSystemService(NotificationManager.class); | NotificationManager notificationManager = context.getSystemService(NotificationManager.class); | ||||
| notificationManager.createNotificationChannel(channel); | notificationManager.createNotificationChannel(channel); | ||||
| } | } | ||||
| } | } | ||||
| public static void createLivePositionsChannel(Context context){ | |||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |||||
| NotificationChannel channel = new NotificationChannel( | |||||
| Notifications.MATO_LIVE_POSITIONS_CHANNEL, | |||||
| context.getString(R.string.live_positions_notification_channel), | |||||
| NotificationManager.IMPORTANCE_MIN | |||||
| ); | |||||
| channel.setDescription(context.getString(R.string.live_positions_notification_channel_desc)); | |||||
| NotificationManager notificationManager = context.getSystemService(NotificationManager.class); | |||||
| notificationManager.createNotificationChannel(channel); | |||||
| } | |||||
| } | |||||
| } | } | ||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register