I have a service gcm in the background, how do I k

2019-09-19 01:52发布

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 ??

3条回答
再贱就再见
2楼-- · 2019-09-19 02:07

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.

查看更多
The star\"
3楼-- · 2019-09-19 02:08

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:

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-09-19 02:19

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

查看更多
登录 后发表回答