将包从服务传递到接收器 return null

passing bundle from service to receiver return null

将包从服务传递到接收方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

主要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();
        }*/
    }

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

那是因为你的HashMap数据是空的。您正在创建一个新的 HasMap 对象并试图从中获取实际上为空的值。

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

尝试使用您在 onMessageReceived 中添加的一些键(应该保存在 R.strings)中使用 extra.getString(key) 而不是 intent.getBundleExtras("hashmap")

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