react-nativesms

How to read incoming SMS in a ReactNative app when app is not running?


There are several ReactNative SMS plugins available but most read SMS when app is in foreground. How can I read SMS when app is not even running, like a native SMS reader app?


Solution

  • I am not aware of any libraries available. But you can do as follows-

    1. Create a BackgroundScanService.java which is an android service, which keeps scanning the incoming sms, when it receives any sms. It broadcasts the sms.
    2. Create BackgroundScanModule.java, this file is the interface between your react native and Java code. Once sms is received in the broadcast receiver it passes that data to headless js. Headless js performs task when app is in the background and it performs action defined by user even when app is killed. For more info on Headless JS check https://facebook.github.io/react-native/docs/headless-js-android

    I had implemented similar service for beacon scanning, you can check it here - https://medium.com/@skytreasure/react-native-beacon-scanner-when-app-is-not-running-android-172077644bc0