I have tried setting a FLAG_NO_CLEAR flag, but when I close the recent applications application, the notification closes. I also tried "ongoing" but the same thing keeps happening. Someone who knows how to fix it?
this my code in main activity:
public fun createNotificationChannel(contextLocal : Context,avalaible:Int,spent:Int) {
val prefs = contextLocal.getSharedPreferences("FlutterSharedPreferences" , Context.MODE_PRIVATE)
var velo = Math.round(Math.round(prefs.getFloat("velocityLocal", 0.toFloat()) * 100.0) / 100.0 * 10.0) / 10.0
modeL = prefs.getString("modeL", "")!!
var distance = Math.round(Math.round((velo * (avalaible.toDouble() / 60.toDouble())) * 100.0) / 100.0 * 10.0) / 10.0 /* CONVERTER DECIMAL 2 DIGITS */
notificationManager = contextLocal.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
/* NOTIFICATIONS */
val snoozeIntent = Intent(contextLocal, ReceiverRepo::class.java).apply {
action = "NOTIFICATION_TAPED"
}
val snoozePendingIntent: PendingIntent =
PendingIntent.getBroadcast(contextLocal, 0, snoozeIntent, 0)
// var action : Notification.Action = Notification.Action.Builder(R.drawable.app_icon, "Stop", snoozePendingIntent).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(channelId,description,NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(contextLocal,channelId)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Approximately : " + distance.toString() + " km")
.setContentText(modeL + " " + "- " + velo+ " km/h " + "in " + avalaible.toString() + " min")
.setSubText("avalaible time : " + (avalaible - spent).toString() + " min")
.setContentIntent(snoozePendingIntent)
/* .setOngoing(true) */
}
notificationManager.notify(1234,builder.build())
}
my BroadCastReceiver:
class ReceiverRepo : BroadcastReceiver() {
override fun onReceive(contextLocal: Context, intent: Intent){
val prefs = contextLocal.getSharedPreferences("FlutterSharedPreferences" , Context.MODE_PRIVATE)
val avalaible = prefs.getLong("flutter.avalaibleTime" , 0)
val spent = prefs.getLong("flutter.timeSpent" , 0)
val switch = prefs.getBoolean("flutter.switched" , false)
val state = prefs.getBoolean("flutter.state", false)
if(avalaible == spent){
with(prefs.edit()) {
/* MODIFY VARS SHARED*/
putBoolean("flutter.state", false)
apply()
}
// close notification
var ns = Context.NOTIFICATION_SERVICE
var nMgr = contextLocal.getSystemService(ns) as NotificationManager
nMgr.cancel(1234)
}
if(intent.action.equals("NOTIFICATION_TAPED")){
Log.d("NOTIFICATION", "LE DI CLICK AL BOTON DE LA NOTIFICACION")
// open the application by clicking
var packageManager = contextLocal.getPackageManager();
// create the attempt, taking into account the package name
val intent1: Intent =
packageManager.getLaunchIntentForPackage("com.example.myProject")!!
contextLocal.startActivity(intent1)
}
if(intent.action.equals("NOTIFICATION_TAPED_FULL")){
}
if(intent.action.equals("INTENT_ALARM") && switch && avalaible > spent){
with(prefs.edit()) {
/* MODIFY VARS SHARED*/
putLong("flutter.timeSpent", spent + 3)
putInt("timeSpentLocal", spent.toInt() + 3)
putLong("flutter.avalaibleTime", avalaible)
putBoolean("flutter.state", true)
apply()
}
/* CONFIGURE WORKER */
try{
TestActivity.workerInitialize(contextLocal)
Log.d("VELOCITY IN BROADCAST" , TestActivity.velocity.toString())
/* setAlarm(contextLocal, 180000) */
}catch(e : Exception){
Log.d("prueba" , e.toString())
TestActivity.createNotificationError(contextLocal)
/* CONFIGURE NEXT ALARM */
}
}
}
fun setAlarm(context : Context , milis: Long) {
lateinit var contextLocal : Context
contextLocal = context
lateinit var alarmManager : AlarmManager
/* Log.d("prueba" , "estoy en el setAlarmmmm") */
alarmManager = contextLocal.getSystemService(Context.ALARM_SERVICE) as AlarmManager
var intent = Intent(contextLocal,ReceiverRepo::class.java)
intent.apply {action = "INTENT_ALARM" }
var pendingIntent = PendingIntent.getBroadcast(contextLocal, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milis, pendingIntent)
}
}
The previous code, puts an alarm if some conditions are met, what I want is to put a fixed notification, which takes the variables, and is removed when it has reached 0