0

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.

GameDevGuru
  • 1,095
  • 2
  • 12
  • 27

1 Answers1

0

I suggest you to have a look at this: Checking if an Android application is running in the background There are several "hacks" to know if the app is running in background, one of them is by taking into account the processes that are running Using the Activity Manager. Another one is to keep tracking of the activities lifecycle. In the link I provided you'll be able to have more detailed information.

Hope this helps.

Community
  • 1
  • 1
  • I set a boolean to know if app is running or not. That's not what I'm asking about though.. – GameDevGuru Feb 09 '14 at 03:50
  • on the other hand, you can create a static method wherever you want, I don't know how you want to notice the user, If it's a toast/dialog or any a kind of view that needs a context, you have can get it with getApplicationContext() in the intent. If you're using a DB, you can also call a static method in the activity and handle the event there, just think what params you'll need for updating the changes in UI (in MainActivity). – Jonathan Fragoso Feb 09 '14 at 15:22