user7113784
user7113784

Reputation:

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());
    }

Upvotes: 0

Views: 448

Answers (4)

Masala Tea
Masala Tea

Reputation: 21

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));
    }

Upvotes: 1

altu
altu

Reputation: 357

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));
        }

Upvotes: 0

Ale
Ale

Reputation: 86

Try extra.getString(key) with some of the keys you added in onMessageReceived (wich should be saved in R.strings) instead of intent.getBundleExtras("hashmap")

Upvotes: 0

Basu
Basu

Reputation: 763

That is because your HashMap data is empty. You are creating a new object of HasMap and trying to get value from it which is actually empty.

HashMap<String, String> data = new HashMap<>();

Upvotes: 0

Related Questions