I've properly set up Google Cloud Messaging for Android and receive notifications from my server. However, I'd like to avoid a notification if the app is open but fetch the data and notify the user within the app. Please look over my GcmIntentService below, where the notification is processed:
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
if(MainActivity.app_is_open){
// I would like to call a function from MainActivity to fetch data here
} else
// Sends notification here
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
I can't reference functions from MainActivity because they are non-static. What is the common approach? I've had trouble finding any questions related to this situation.