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

2
  • 1
    I'm going to assume this is on a phone (as it really isn't clear from the question) so the question then becomes which platform (rules about what can run in the background differ by OS)
    – hardillb
    Sep 8, 2022 at 12:35
  • I'm working on android platform
    – Kindth
    Sep 8, 2022 at 15:32

2 Answers 2

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.