Foreground service notification - android

I'm trying to create a foreground service that check devices location every minute and I've managed to make it work. But I have some problems regarding the obligatory notification about the service being active.
I can't get the notification badge to stop showing and I can't make the notification minimized programatically.
Here are the relevant parts:
class LocationPollingService : Service() {
companion object {
const val NOTIF_ID = 2
const val NOTIF_CHANNEL_ID = "background_service_notification"
const val NOTIF_CHANNEL_NAME = "Background service"
}
#Nullable
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(
intent: Intent?,
flags: Int,
startId: Int
): Int {
createBackgroundNotifChannel()
startForeground()
requestLocation()
return super.onStartCommand(intent, flags, startId)
}
private fun startForeground() {
val notificationIntent = Intent(this, SplashActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0,
notificationIntent, 0
)
val notification = NotificationCompat.Builder(
this,
NOTIF_CHANNEL_ID
)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_app_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.im_watching_you))
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIF_ID, notification.build())
} else {
notification.priority = NotificationCompat.PRIORITY_MIN
startForeground(NOTIF_ID, notification.build())
}
}
private fun createBackgroundNotifChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelBackground = NotificationChannel(
NOTIF_CHANNEL_ID,
NOTIF_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
).apply {
description = getString(
R.string.notification_channel_background_description
)
enableLights(false)
enableVibration(false)
setShowBadge(false)
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channelBackground)
}
}
}
If I understand correctly, IMPORTANCE_LOW is supposed to be used for this kind of notification and it is supposed to make it minimized by default. The notification is never minimized nor can I minimize it by clicking. The only way I can make it minimized is by manually changing notification channel settings in device settings (setting Notification style to Silent and minimized, instead of just silent, which is the default). I tried setting the importance to IMPORTANCE_MIN and IMPORTANCE_NONE, but that didn't change anything.
As mentioned, the second problem is that the badge is shown (the notification counter displayed on app icon), despite of setShowBadge(false).
What am I missing?

You need to notify notification after build notification like below :
notificationManager.notify(NOTIF_ID , notification.build());

for hiding notification badge from statusbar
you should change notification channel priority:
val channelBackground = NotificationChannel(
NOTIF_CHANNEL_ID,
NOTIF_CHANNEL_NAME,
NotificationManager.IMPORTANCE_MIN

Related

Android Wear startForeground not displaying notification

I am writing an Android Wear app that uses a foreground service.
I have verified that the foreground service wokrs, and the code inside the service executes as needed (SensorEventListener) and I even use intents to display information in the UI.
However, the notification for the service is not posting in the list of notifications. I use a Galaxy Watch4 for testing.
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
broadcaster = LocalBroadcastManager.getInstance(this)
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, FLAG_IMMUTABLE)
val notificationChannel = NotificationChannel(foregroundChannelId, "Foreground Service", NotificationManager.IMPORTANCE_HIGH)
notificationChannel.lightColor = Color.BLUE
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(notificationChannel)
val notification = NotificationCompat.Builder(this, foregroundChannelId).setOngoing(true)
.setContentTitle("Monitoring sensors").setContentText("app_name is actively monitoring your biosensors.")
.setContentIntent(pendingIntent).setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(R.mipmap.ic_launcher).build()
startForeground(1, notification)
prepareSensors()
return START_NOT_STICKY
}
There is no sticky notification in the notification panel.
I've tried removing the .setOngoing.
Maybe it was delayed in showing, as the operation was too quick ?
Try adding this:
builder.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)

How to create multiple notifications in Kotlin in foreground service

I am working on a parental control app which notify parent multiple times but when I try to create notification with a background service it generates only one 1.
Here is how I do it:
fun createNotification(parent_name: String, notificationText:String, id: Int){
val MchannelId = channelId+id.toString()
if (Build.VERSION.SDK_INT >= 26) {
val channel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel(
MchannelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT
)
} else {
TODO("VERSION.SDK_INT < O")
}
(getSystemService(NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(
channel
)
}
val notificationIntent = Intent(this, TabbedActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
id, notificationIntent, 0
)
val notification: Notification = NotificationCompat.Builder(this, "$MchannelId")
.setContentTitle("Hi $parent_name")
.setContentText(notificationText)
.setSmallIcon(R.drawable.icon_child)
//.setContentIntent(pendingIntent)
.build()
startForeground(random_number, notification)
}
My Full-Service Class:
const val TAG2 = "Child Service"
class ParentService: Service() {
val db = FirebaseFirestore.getInstance()
private val channelId = "Notification from Service"
var parent_name = userName
override fun onBind(intent: Intent?): IBinder? = null
//OnBind Function Implementation
init {
Log.d(TAG2, "Started Service!")
}
//onCreate Method Implementation
override fun onCreate() {
super.onCreate()
}
//OnStartCommand Override
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Thread{
while (true){
checkStatus()
Thread.sleep(PARENT_CHECK_TIME)
}
}.start()
return START_STICKY
}
private fun checkStatus() {
var listOfNames = ""
var i = 1
val calendar: Calendar = Calendar.getInstance()
var list = ArrayList<String>()
db.collection(LINKED_CHILDS)
.whereEqualTo(USER_PHONE, userPhone)
.get()
.addOnSuccessListener { documents ->
for (document in documents){
val startTime: Long = calendar.getTimeInMillis()
val diff = startTime - (document.data[ACTIVE_STATUS] as Long)
Log.d("TAG", "Time Difference : $diff")
Log.d("TAG", "${document.data[USER_NAME].toString()}")
if (diff> MAX_GAP_TIME){
Log.d("TAG", "Entered IFF")
list.add(document.data[USER_NAME].toString())
}
}
for (name in list){
listOfNames = listOfNames + "$i. Your child $name is not active\n"
i++
createNotification(parent_name, listOfNames, i)
Log.d("TAG Notification ID:", "ID: $i")
}
Log.d("TAG: ", "$listOfNames")
}
}
fun createNotification(parent_name: String, notificationText:String, id: Int){
val MchannelId = channelId+id.toString()
if (Build.VERSION.SDK_INT >= 26) {
val channel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel(
MchannelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT
)
} else {
TODO("VERSION.SDK_INT < O")
}
(getSystemService(NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(
channel
)
}
val notificationIntent = Intent(this, TabbedActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
id, notificationIntent, 0
)
val notification: Notification = NotificationCompat.Builder(this, "$MchannelId")
.setContentTitle("Hi $parent_name")
.setContentText(notificationText)
.setSmallIcon(R.drawable.icon_child)
//.setContentIntent(pendingIntent)
.build()
startForeground(id, notification)
}
}
Kinldy let me know how I can create multiple Notifications using this background service. Thank You so much in advance!
Kinldy let me know how I can create multiple Notifications using this background service. Thank You so much in advance!
Kinldy let me know how I can create multiple Notifications using this background service. Thank You so much in advance!
If you create a non-persistent notification, it will show your notifications. The permanent notification will be used for your service to run in the background.
#RequiresApi(Build.VERSION_CODES.O)
private fun createNotification() {
val intent = Intent(this, TabbedActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon_child)
.setContentTitle("Hi $parent_name")
.setContentText(notificationText)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
with(NotificationManagerCompat.from(this)) {
notify(notifManagerId, notification.build())
notifManagerId++
}
parmanentNotification()
}
this is a permanent notification will not be lost and destroyed will keep the service running permanently
private fun parmanentNotification() {
val notification=NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.drawable.icon_child)
.setContentTitle("Hi $parent_name")
.setContentText("Application service running in the background")
.build()
startForeground(1,notification)
}
you aren't creating a common Notification in this scenario, you are running a Service, which must have a foreground representation on screen. So Activity visible or sticked, fixed Notification, and you are showing it
Now you can have much Notifications using similar code, but don't show them using startForeground, instead use NotificationManager, preferably compat version
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(uniqueId, notification);
or just like you are using it already when creating channel inside if: (getSystemService(NOTIFICATION_SERVICE) as NotificationManager).notify(...)
foreground-related Notification is sticky and lives as long as Service works in background, they are "tied". other Notifications may be configured to be sticky or swipeable, also should be posted on some own Channel (per child? per action?). Note that if you show yet another sticky Notification then you have to release it by own through code, just killing Service won't dismiss it as it does with foreground-related Notification
some DOC in here, read carefully, all answers are there

Can't silence notification on Android API29

Need to silence a notification that is shown by a foreground service. To silence the notification I have used many suggested solution from SO. But nothing seems to silence the annoying notification sound. It is silenced on SDK25. But on SDK 29 emulator it is not silenced. What can be the solution?
Code:
//service
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotificationChannel()
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, 0
)
val notification: Notification =
NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("app_name")
.setContentText("Updating database.")
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notification)
thread() {
Thread.sleep(3000)
stopSelf()
}
return START_NOT_STICKY
}
override fun onDestroy() {
super.onDestroy()
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = SettingsHelper.KEY_NOTIFICATION_channel_name_service
val description = SettingsHelper.KEY_NOTIFICATION_CH_ID_SERVICE
val channel = NotificationChannel(
SettingsHelper.KEY_NOTIFICATION_CH_ID,
name,
NotificationManager.IMPORTANCE_NONE
)
channel.description = description
channel.setSound(null, null)
val manager = getSystemService(
NotificationManager::class.java
)
manager.createNotificationChannel(channel)
}
}
Could you use setSilent() from NotificationCompat.Builder
If true, silences this instance of the notification, regardless of the sounds or vibrations set on the notification or notification channel. If false, then the normal sound and vibration logic applies. Defaults to false.
Note, that if you are changing notification settings, I recommend you to uninstall and re-install the app everytime since it might not be updated the settings you set in the new run.

Prevent a notification going away from notification bar [Android]

First of all, I am sorry if there is a topic on this but I didn't found the exact solution.
The problem is that, I am setting a notification but when I close the app, the notification is removed automatically by the system. You might think it is relevant to onGoing attribute but as far as I know it is required user interaction. In my case, notifications disappear by itself.
Here is the code:
class NotificationHelper {
private val CHANNEL_ID: String = "Default Channel ID"
private var context: Context
private var notificationManager: NotificationManager
private var notificationBuilder: NotificationCompat.Builder
constructor(context: Context) {
this.context = context
this.notificationManager =
this.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// NotificationChannel is required on Oreo and newer
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.notificationManager.createNotificationChannel(
NotificationChannel(
CHANNEL_ID,
this.context.getString(R.string.channel_name),
NotificationManager.IMPORTANCE_HIGH
)
)
}
notificationBuilder = NotificationCompat.Builder(this.context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_set_notification_black)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setNotificationSilent()
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(
PendingIntent.getActivity(
this.context, 0, Intent(
this.context,
MainActivity::class.java
), 0
)
)
}
fun setNotification(notification: Notification) {
val deleteIntent = Intent(context, BootOrUpdateBroadcastReceiver::class.java)
deleteIntent.action = GlobalInfo.ACTION_NOTIFICATION_DELETE
deleteIntent.putExtra(GlobalInfo.NotificationEntry.COLUMN_ID, notification.id)
val notificationToBeShown: android.app.Notification = this.notificationBuilder
.setContentTitle(notification.title)
.setContentText(notification.content)
.setStyle(NotificationCompat.BigTextStyle().bigText(notification.content))
.setAutoCancel(notification.removeByClick)
.setOngoing(!notification.removeBySwipeAway)
.setDeleteIntent(
PendingIntent.getBroadcast(
context,
0,
deleteIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)
)
.build()
with(NotificationManagerCompat.from(this.context)) {
// notificationId is a unique int for each notification that you must define
notify(notification.id, notificationToBeShown)
}
}
fun clearNotification(notificationId: Int) {
this.notificationManager.cancel(notificationId)
}
fun clearAllNotifications() {
this.notificationManager.cancelAll()
}
}
Also, I thought to use a sticky service but it doesn't make sense to me. Services are also being destroyed as the application is closed. There should be another way.
Thanks in advance :)

How to do custom turn on/off push notification in Background and Kill

I need to develop custom turn off/on push notification. Value "on" or "off" I get from SharedPreferences.
In Foreground the option of turn off works well because it can set in onMessageReceived.
But how to do this options for Kill and Background modes?
For sending push notifications I use Postman.
My code in FMC:
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val CURRENT_PUSH = "currentPush"
private var sPref: SharedPreferences? = null
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
if(loadCurrentPushNotification()) {
if (remoteMessage!!.data != null) sendNotification(remoteMessage)
}
}
private fun sendNotification(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
val title = data["title"]
val content = data["content"]
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val NOTIFICATION_CHANNEL_ID = "1234"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant") val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
"SA Notification",
NotificationManager.IMPORTANCE_MAX)
notificationChannel.description = "SA channel notification"
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
}
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sa_launcher)
.setContentTitle(title)
.setContentText(content)
.setContentInfo("Breaking")
notificationManager.notify(1, notificationBuilder.build())
}
private fun loadCurrentPushNotification(): Boolean { //to read the status push notification from SharedPreferences
sPref = getSharedPreferences("pushesOnOff", Context.MODE_PRIVATE)
return sPref!!.getBoolean(CURRENT_PUSH, true)
}
}
I suspect that your push notification payload/content contains the notification key like
notification : {
'title':'This is title'
}
If this is true, then you will not receive the onMessageReceived callback when your app is killed and hence your check will not work. In order to prevent this, remove the notification from your payload and only send data and it should work without any change on android side.
You can read about this behavior HERE.

Categories

Resources