2

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?

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
  • You can broadcast the data from Broadcastreceiver to activity.Register a receiver in Activity in OnCreate and unregister in onDestroy.Also you can add a variable in SharedPreference to update everytime activity resumed , active ,paused also onDestroy. Same u can check from Your broadcast receiver from sending data to activity. – Vijay Mar 31 '16 at 08:11
  • But , i think it will not run or notify when application is close . – Zahidul Islam Mar 31 '16 at 08:26
  • In you function createNotification(String body) > just check the Value from SharedPreference for Activity State and if activity is running then pass data to it otherwise showNotification. – Vijay Mar 31 '16 at 08:35
  • Also you can try this http://stackoverflow.com/questions/18038399/how-to-check-if-activity-is-in-foreground-or-in-visible-background – Vijay Mar 31 '16 at 08:37

1 Answers1

1

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.

reixa
  • 6,903
  • 6
  • 49
  • 68