1

What is the proper way to show notifications of the message's received from mqtt when app is running in the background or when app is closed using react native.

So far I have achieved to show notifications when app is in the foreground using react-native-paho-mqtt as mqtt client and expo-notifications for showing notifications

Kindth
  • 179
  • 10

2 Answers2

2

The only way to make sure notifications are shown is to use a server and the relevant platform’s push notifications.

You’ll have a hard time keeping a process running, active, and connected in the background. Even if it’s possible on Android (not quite sure) it definitely won’t be possible on iOS.

So your app needs to register for notifications and send the relevant token to the server.

Your server (possibly the same one running the MQTT broker) will then need to send notifications to your phone using that token whenever needed. How that process is triggered is up to you. You could have a client running on the server receiving the MQTT messages and triggering the notifications, or it could be some sort of integration directly in the broker.

jcaron
  • 2,408
  • 5
  • 10
1

I'll start with saying I've never worked with React or React Native.

But you are going to need what Android calls a Background Service to run the MQTT client in so it can stay alive in the background. 30 seconds of Googling threw up https://github.com/Rapsssito/react-native-background-actions as a possibly solution, but you will need to work out how to run the MQTT client in that.

hardillb
  • 12,813
  • 1
  • 21
  • 34