Using two or more GCM Intent Services in one application

I am writing an application that integrates with Smooch and Carnival . Both of these libraries receive GCM push messages, using a standard approach to define the GCM Intent service to receive messages.

When I use only Smooch, everything works fine. When I use only Carnival, everything works fine. The problem occurs when I try to use both. I found that the GCM receiver will simply start the first service specified in the manifest, which determines the intent of com.google.android.c2dm.intent.RECEIVE .

In fact, I found that the order in which the libraries in my build.gradle are listed affects the order in which their manifests merge into the application manifest. So, if I apply smooch first, it works (but the carnival doesnโ€™t get anything). And if I install Carnival first, it works (but Smooch never gets anything).

How can I handle multiple GCM intent services when I do not control any of them? In general, how should applications define and manage multiple GCM intent services?

+5
source share
2 answers

The reason you cannot make it work in both Carnival and Smooch is because both libraries register their own GcmListenerService, and in Android the first GcmListenerService defined in your manifest will receive all GCM messages.

I have a solution for you, based primarily on the following SO article: Multiple GCM listeners using GcmListenerService

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

To specify your own GcmListenerService, follow the instructions in the Google Cloud Messaging Documentation .

Smooch provides the tools you need to disable GCM internal registration when you have your own.

To do this, simply call setGoogleCloudMessagingAutoRegistrationEnabled when initializing Smooch:

 Settings settings = new Settings("<your_app_token>"); settings.setGoogleCloudMessagingAutoRegistrationEnabled(false); Smooch.init(this, settings); 

And in your GcmRegistrationIntentService call Smooch.setGoogleCloudMessagingToken(token); with your token.

Once this is completed, you can send the GCM message to any GCM recipient that you want.

 @Override public void onMessageReceived(String from, Bundle data) { final String smoochNotification = data.getString("smoochNotification"); if (smoochNotification != null && smoochNotification.equals("true")) { data.putString("from", from); Intent intent = new Intent(); intent.putExtras(data); intent.setAction("com.google.android.c2dm.intent.RECEIVE"); intent.setComponent(new ComponentName(getPackageName(), "io.smooch.core.GcmService")); GcmReceiver.startWakefulService(getApplicationContext(), intent); } } 

EDIT

Starting with version 3.2.0 of Smooch, now you can more easily trigger a Smoochs notification by calling GcmService.triggerSmoochGcm in your onMessageReceived.

 @Override public void onMessageReceived(String from, Bundle data) { final String smoochNotification = data.getString("smoochNotification"); if (smoochNotification != null && smoochNotification.equals("true")) { GcmService.triggerSmoochGcm(data, this); } } 
+6
source

Do you use gradle dependencies? You will have to download both libraries and use them as modules, they probably use the same services, when you load them you can change the name of the service and solve any problem that can be fixed.

I assume that you will probably have to create a GCM broadcast receiver using your application module (even if it calls libs services).

0
source

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


All Articles