user2616112
user2616112

Reputation: 31

I have a service gcm in the background, how do I know if the app is open or not?

I have a class that extends GCMBaseIntentService, when I get a message from gcm function:

@Override

 protected void onMessage (Context context, Intent intent)
 {
     String message = intent.getExtras().GetString("alien");
     generateNotification (context, message);
 }

I wish that when a new message arrives the app recognize if the application is open and the user is using, or is a simple background service, and therefore I see a notification. I need to know if it is in the background or foreground, any suggestions ??

Upvotes: 0

Views: 579

Answers (3)

sddamico
sddamico

Reputation: 2130

A pretty common approach to solving this problem is using an event bus to publish the message to the rest of your app to see if anyone is registered to handle it.

A good event bus for Android is the greenrobot EventBus https://github.com/greenrobot/EventBus

A code example of how to do it:

Create a class for your message

public class MessageEvent { 
    public message;

    public MessageEvent(String message) {
        this.message = message;
    }
}

Add the EventBus to your BroadcastReceiver

protected void onMessage (Context context, Intent intent) {
    String message = intent.getExtras().getString("alien");

    MessageEvent event = new MessageEvent(message);

    EventBus.register(this);
    EventBus.getDefault().post(event)
}

public void onEvent(NoSubscriberEvent event) {
    if (event.originalEvent instanceOf MessageEvent) {
        generateNotification(((MessageEvent) event.originalEvent).message));
    }
}

Then, in your Activity:

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

// This method will be called on the main thread when a MessageEvent is posted
public void onEventMainThread(MessageEvent event){
    Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show();
}

This way, if your activity is active, it can process the MessageEvent, if it is not active, you can display the notification as usual.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006614

when a new message arrives the app recognize if the application is open and the user is using, or is a simple background service, and therefore I see a notification

To do that, you can use an in-process event bus. Have the service post an event to the bus. Have the UI subscribe to the bus for those events when the UI is in the foreground (e.g., register in onResume(), unregister in onPause()). Have the UI process the events when the UI gets them. If the UI does not respond to the event, the service can then raise a Notification.

I have sample apps that demonstrate this for three popular event bus implementations for Android:

Upvotes: 4

sridhar venkatesan
sridhar venkatesan

Reputation: 57

GCM runs completely at background and not in foreground.

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

You will use these two permission's which is to receive the message at background.

Please read this.

https://developer.android.com/google/gcm/client.html

Upvotes: 0

Related Questions