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"; | ||||
//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); | ||||
String description = context.getString(R.string.default_notification_channel_description); | String description = context.getString(R.string.default_notification_channel_description); | ||||
int importance = NotificationManager.IMPORTANCE_DEFAULT; | int importance = NotificationManager.IMPORTANCE_DEFAULT; | ||||
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID, name, importance); | NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID, name, importance); | ||||
Show All 23 Lines | public static void createNotificationChannel(Context con, String name, String description, int importance, String ID){ | ||||
notificationManager.createNotificationChannel(channel); | notificationManager.createNotificationChannel(channel); | ||||
} | } | ||||
} | } | ||||
public static Notification makeMatoDownloadNotification(Context context,String title){ | public static Notification makeMatoDownloadNotification(Context context,String title){ | ||||
return new NotificationCompat.Builder(context, Notifications.DB_UPDATE_CHANNELS_ID) | return new NotificationCompat.Builder(context, Notifications.DB_UPDATE_CHANNELS_ID) | ||||
//.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), Constants.PENDING_INTENT_FLAG_IMMUTABLE)) | //.setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), Constants.PENDING_INTENT_FLAG_IMMUTABLE)) | ||||
.setSmallIcon(R.drawable.bus) | .setSmallIcon(R.drawable.ic_bus_stilized_transparent) | ||||
.setOngoing(true) | .setOngoing(true) | ||||
.setAutoCancel(true) | .setAutoCancel(true) | ||||
.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 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){ | |||||
return makeMatoDownloadNotification(context, context.getString(R.string.mqtt_notification_text)); | |||||
} | |||||
public static void cancelNotification(Context context, int notificationID){ | |||||
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |||||
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 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