首页 > 解决方案 > 当应用程序未运行时,如何在 ReactNative 应用程序中读取传入的短信?

问题描述

有几个 ReactNative SMS 插件可用,但大多数在应用程序处于前台时读取 SMS。当应用程序甚至没有运行时,我如何阅读短信,比如原生短信阅读器应用程序?

标签: react-nativesms

解决方案


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


推荐阅读