将广播从本机服务发送到本机模块React Native

时间:2018-09-12 19:58:40

标签: 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);
    }
}

0 个答案:

没有答案