androidservicebroadcastreceiverlocalbroadcastmanager

passing bundle from service to receiver return null


passing bundle from service to receiver return null

i want to pass data in bundle from service to receiver to change ui data instantly

i have pass data bundle as hashmap key and get the same in activity class but always get null i just register registerReceiver in code not in manifest

main activity

BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (intent != null) {

                    Bundle extras = intent.getBundleExtra("hashmap");
                    Toast.makeText(context, extras + "  " + intent.getBundleExtra("hashmap"), Toast.LENGTH_SHORT).show();


                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };


    @Override

    protected void onResume() {
        super.onResume();
        try {
            LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(FIRMessagingService.REQUEST_ACCEPT));
        } catch (Exception e) {
            e.printStackTrace();

        }
       /* try {
            registerReceiver(receiver, new IntentFilter(FCMActivity.NEW_NOTIFICATION));
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);


        } catch (Exception e) {

        }
        /*try {
            unregisterReceiver(receiver);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }

service(Firebase)

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.e("Msg", remoteMessage.getData().size() + "");
    //sendNotification(remoteMessage.getData());
    try {
        LocalBroadcastManager broadcaster = LocalBroadcastManager.getInstance(getBaseContext());
        HashMap<String, String> data = new HashMap<>();
        Bundle msg = new Bundle();
        for (String key : data.keySet()) {
            Log.e(key, data.get(key));
            msg.putString(key, data.get(key));
        }

        Intent intent = new Intent(REQUEST_ACCEPT);
        intent.setAction(REQUEST_ACCEPT);
        intent.putExtra("hashmap", msg);

        broadcaster.sendBroadcast(intent);


    } catch (Exception e) {
        Log.e("error" + "receiver", e.toString());
    }

Solution

  • its because u have just initialised it.but never pass data to the map object. so map is always null . try below code

     Map<String, String> data = remoteMessage.getData();
    
    
            Bundle msg = new Bundle();
            for (String key : data.keySet()) {
                Log.e(key, data.get(key));
                msg.putString(key, data.get(key));
            }