android - 从本机服务发送广播到本机模块 React Native
问题描述
我正在尝试将广播从后台服务发送到本机模块,然后做出本机反应,但我无法让本机模块中的广播接收器接收广播。我的服务接收推送通知正常,发送广播时没有错误。
我的服务看起来像这样
public myService extends CustomService{
public void sendBroadcastFunction(){
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this.getApplicationContext());
Intent intent = new Intent("com.appName.NOTIFICATION");
localBroadcastManager.sendBroadcast(intent);
}
}
我的原生模块
public class myModule extends ReactContextBaseJavaModule{
private BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
Log.d("Houston", "we possibly have a notification");
if(intent != null){
Log.d("Houston", "we have a notification");
Log.d("Houston", "broadcast receiver is working");
//Toast.makeText(getReactApplicationContext(), "Notification received", Toast.LENGTH_LONG).show();
}
}
};
public myModule(ReactApplicationContext reactContext){
super(reactContext);
reactContext.startService(newIntent(reactContext, myService.class));
IntentFilter filter = new IntentFilter("com.appName.NOTIFICATION");
reactContext.registerReceiver(receiver, filter);
}
}
解决方案
推荐阅读
- nginx - 用 nginx rewrite 重写一个特定的参数
- python - 当有(经度、纬度、月、年)数据集时,用轮廓绘制 xarray
- vue.js - 为什么我的 Nuxt 应用程序在 PM2 下意外关闭?
- node.js - 动态 Web 抓取未返回完整的 NodeList
- c++ - std::bind 默默地放弃了参数?
- c# - 没有链接表/模型的多对多关系?
- mysql - 仅基于特定分隔符拆分字符串
- javascript - 云功能测试失败“无效请求,无法处理”。
- c++ - 删除使用 C++17 pmr 内存资源分配的多态对象
- reactjs - ant design - 带时间的日期选择器将 00:00:00 保存为时间?