Search code examples
androidgoogle-cloud-messagingandroid-broadcastreceiver

Send Receiver data to Activity


I want to share GCM receiving data from the Receiver to Activity. If the Activity is active/onResume/onPause state, I want to show the data to the activity. If the activity destroyed then I want to show that information in Notification bar.

Receiver -> GcmMessageHandler.class

import android.os.Bundle;
import com.google.android.gms.gcm.GcmListenerService;

public class GcmMessageHandler extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        createNotification(message);
    }

    private void createNotification(String body) {
        String sendDataToActivity = body; // This is value i want to pass to activity
    }

}

Activity -> MainActivity.class

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    String RECEIVER_VALUE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);

        textView.setText(RECEIVER_VALUE); // Here i want to set the receiver valiue
    }
}

And here the notification function.

private void Notify(String notificationMessage){
      Notification notification = new Notification.Builder(getBaseContext())
            .setContentText(notificationMessage)    // Show the Message Here
            .setSmallIcon(R.drawable.ic_menu_gallery)
            .setWhen(System.currentTimeMillis())
            .build();

    notification.notify();
}

I already use the intent to receive from onNewIntent method in Activity.

from Receiver ->

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("message",message);
getApplicationContext().startActivity(intent);

and from Activity ->

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String message =  intent.getExtras().getString("message").toString();
    textView.setText(message);
}

But the problem is, if my activity is close it reopen the activity. Any other solution except this?


Solution

  • You can use EventBus for that like this:

    In your Application class

    @Override
    public void onCreate() {
        super.onCreate();
    
        EventBus.builder()
                .logNoSubscriberMessages(false)
                .sendNoSubscriberEvent(false)
                .throwSubscriberException(false)
                .installDefaultEventBus();
    
    }
    

    Add this method on your Activity, this will be called everytime you make a post through EventBus.

    @Subscribe
    public void onEventFromReceiver(ReceiverEvent event){
        Log.d(TAG, event.message);
    }
    

    On your "send data to Activity" method

    EventBus.getDefault().post(new ReceiverEvent(message));
    

    And your ReceiverEvent class

    public class ReceiverEvent {
    
        public final String message;
    
        public ReceiverEvent(String message) {
            this.message = message;
        }
    
    }
    

    This way, if the activity is not visible or the app is on background, nothing will happen.

    This is the EventBus project

    Hope it helps.