Several GCM listeners using the GcmListenerService

Google recommends using GcmListenerService instead of GcmBroadcastReceiver.

We make a library project (com.android.library plugin) that uses GCM. It works well when the Application project (com.android.application plugin) DOES NOT use GCM for its use.

When the application project uses GCM for it, the library project receiver is not called!

The library module defined GCM permissions and receiver and service entries in the manifest. The application module defined its service record in its own manifest.

With the GcmBroadcastReceiver, this might be possible, since this is a broadcast. There were a lot of questions. Do not want to use GcmBroadcastReceiver due to fatigue.

Is there any way to make this work?

Edit:. Which service will be called depends on the order in which it is defined in the combined xml manifest. If I canceled the order, another Service will be called! Is there a way to call both services?

+4
source share
2 answers

A better solution would be to simply have one implementation of GcmListenerService and have these messages for both.

If you really want to have two implementations, make one main implementation, increasing its priority ( see here ). You can then force this service to invoke the second, creating an intent with data, explicitly installing the component, and then starting another service.

+1
source

@morepork answered correctly. Just answer this to add a piece of code.

This, of course, is not an ideal solution, but at the moment I have not found the best.

This is what my code looks like:

@Override public void onMessageReceived(String from, Bundle data) { if(!"MY_SENDER_ID".equals(from)) { data.putString("from", from); Intent intent = new Intent(); intent.putExtras(data); intent.setAction("com.google.android.c2dm.intent.RECEIVE"); intent.setComponent(new ComponentName(getPackageName(), "my.application.packageId.MyGcmListenerService")); GcmReceiver.startWakefulService(getApplicationContext(), intent); } 

You must make sure that all Service Listeners defined in the joint manifest are run, and this Listener has the highest priority. I know what a limitation.

Edit: Just decompiled the com.google.android.gms.gcm.GcmReceiver class, it does nothing good than the previous GcmBroadcastReceiver. Just update the token. So it's best to use GcmBroadcastReceiver only when you need multiple callbacks.

+4
source

Source: https://habr.com/ru/post/1246836/


All Articles