Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreground service notification not removed even if the service is stopped #442

Open
mdrafi786 opened this issue Apr 8, 2023 · 13 comments · May be fixed by #444
Open

Foreground service notification not removed even if the service is stopped #442

mdrafi786 opened this issue Apr 8, 2023 · 13 comments · May be fixed by #444

Comments

@mdrafi786
Copy link

Hi there,

I have started the Mqtt connection as a foreground service and on logout, I want to close the MQTT and stop the foreground service. I called stopForeground(true) and stopSelf() to stop my Mqtt service. On destroy of service called but Foreground service notification is not removed from the notification tray. Please check. I have tried to remove the notification but have not been able to do that. Please please help me. I have used the latest version of your library Thanks in advance.

@mdrafi786
Copy link
Author

I am still waiting for a response. Please check this issue.

@hannesa2
Copy link
Owner

Sure, we all want something, but how can you help or contribute ?
Pull requests are welcome, the code is public, and this topic is somehow wired .
Btw, I try to find a solution, but I can't promise anything.

@hannesa2
Copy link
Owner

hannesa2 commented Apr 11, 2023

Some are waiting much longer
#250
#251
#258

When you are urgent waiting, I can recommend to help with it. This prevents from waiting for ever.

@hannesa2 hannesa2 linked a pull request Apr 11, 2023 that will close this issue
@hannesa2
Copy link
Owner

Please use https://github.com/hannesa2/paho.mqtt.android/releases/tag/3.6.0-beta7 and please tell me, how it works after several connect/disconnect. I didn't tested this.

@hannesa2
Copy link
Owner

@mdrafi786 I will not merge #444 when you don't confirm it

@mdrafi786
Copy link
Author

mdrafi786 commented Apr 27, 2023 via email

@Elswah
Copy link

Elswah commented Jan 11, 2024

@mdrafi786 did you resolve this and what is the version working with you

@hannesa2
Copy link
Owner

Why i need to confirm?

@mdrafi786
Because you need it, so you probably have the motivation to test it.

@Elswah
Copy link

Elswah commented Jan 11, 2024

@hannesa2 not working

@mdrafi786
Copy link
Author

mdrafi786 commented Jan 11, 2024 via email

@fattyCoderHK
Copy link

I have only verified the below solution on basicSample.

At MqttAndroidClient.kt disconnect() functions, add

context.unbindService(serviceConnection)

At MQTTExampleActivity, add lifecycle function

override fun onDestroy() {
    Log.d(TAG, "onDestroy")
    mqttAndroidClient!!.disconnect()
    super.onDestroy()
}

Then when activity is destroyed such as press back key within the app, notification will be removed. Pressing Home, it will not.

Some extra modification for basicSample is that at addToHistory, I need to change :
1.
runOnUiThread(Runnable {
this.adapter.add("$timestamp $mainText")
})

  1. For Android 14,
    val channel = NotificationChannel(CHANNEL_ID, "MQTT", NotificationManager.IMPORTANCE_LOW)
    channel.description = "MQTT Example Notification Channel"
    channel.enableLights(true)
    channel.lightColor = Color.GREEN
    channel.enableVibration(false)
    val notificationManager = getSystemService(NotificationManager::class.java)
    notificationManager.createNotificationChannel(channel)

     val notificationBuilder: Notification.Builder =
         Notification.Builder(this, CHANNEL_ID)
             .setContentTitle("MQTT Notification")
             .setContentText("Content title")
             .setSmallIcon(R.drawable.ic_topic)
     val foregroundNotification = notificationBuilder.build()
     mqttAndroidClient = MqttAndroidClient(applicationContext, serverUri, clientId).apply {
         setForegroundService(foregroundNotification)
     }
    

@fabmazz
Copy link
Contributor

fabmazz commented Feb 15, 2024

Hello, I've been using the library on my open source app (https://gitpull.it/source/libre-busto/) for downloading data using MQTT, and since recently I have upgraded the version of your library, using the notification.

I have the same problem with closing the notification, on devices running Android 11 and 14.

I use the AndroidMQTTClient inside an MQTT-specific task, but I call the AndroidMQTTClient.close() method in my class.

You can try the app yourself using the build available at https://gitpull.it/D156

@hannesa2
Copy link
Owner

I have only verified the below solution on basicSample.

@fattyCoderHK
I added some suggestions with this #562
Btw, basicSample is not really maintained, because it's not 100% working (that's why it's not well tested by Espresso) But I'm happy for any help here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants