我的应用如何知道 android 设备是否收到了新消息

How Can my app know if android device has received a new message

我是 React-Naive 的新手,正在尝试构建一个使用 react-native-get-sms-android 通过应用程序发送消息的应用程序。

我可以从收件箱发送读取短信,但是没有事件在包裹中知道是否收到了新的短信。

react native中是否有任何EventAPI ? 我想我将不得不使用 轮询 来检查是否有新的短信到达。

您可以创建自己的本机模块或使用 react-native-android-sms-listener 来侦听类似于 Whatsapp.

的新消息

docs

中提供了手动安装

用法

从 'react-native-android-sms-listener'

导入 SmsListener
componentDidMount() {
  this.subscription = SmsListener.addListener(message => {
    console.info(message)
  })
}

componentWillUnmount() {
  this.subscription.remove()
}

其中内容的格式

{
  originatingAddress: string,
  body: string
}

还要确保您需要特定权限才能阅读 android 的短信,请查看权限文档 here