FATAL EXCEPTION: MQTT Rec: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

I am getting fatal exception while receiving the data from MQTT I have changed my all legacy code to new code where I am passing RECEIVER_NOT_EXPORTED but still, I am facing the same issue.

ContextCompat.registerReceiver(this,mMessageReceiver,
                new IntentFilter(AppConstants.INTENT_FILTERS.DASHBOARD_MESSAGE),
                ContextCompat.RECEIVER_NOT_EXPORTED);

The same code is working fine in the below version of 14.

Can anyone please help me in this?

I have tried to pass RECEIVER_NOT_EXPORTED while registering Broadcast receiver but still, I am facing the same issue

1

There are 1 best solutions below

0
Laser On

Instead of using contextCompat use this if it is an activity or use requireContext() if it is a fragment.

Then use it like this.

requireContext().registerReceiver(mMessageReceiver,
                new IntentFilter(AppConstants.INTENT_FILTERS.DASHBOARD_MESSAGE),RECEIVER_NOT_EXPORTED);

Make sure you import the correct one. You need to import the context one.

enter image description here

You can even add an sdk check.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
     requireContext().registerReceiver(mMessageReceiver,
                new IntentFilter(AppConstants.INTENT_FILTERS.DASHBOARD_MESSAGE),RECEIVER_NOT_EXPORTED);
}else {
    requireContext().registerReceiver(mMessageReceiver,
                new IntentFilter(AppConstants.INTENT_FILTERS.DASHBOARD_MESSAGE));
}