GCM Integration (legacy)

This guide addresses the GCM integration

Implementation

In order to keep GCM libraries working into your Android application for Pushwoosh SDK 5.1.0+ follow the steps below:

1. Include the following repositories in project's build.gradle:

allprojects {
    repositories {
        jcenter ()
        google ()
    } 
}

2. Add pushwoosh, pushwoosh-gcm, android support v4 and google play services library dependencies into your application build.gradle:

implementation 'com.pushwoosh:pushwoosh:+'
implementation 'com.pushwoosh:pushwoosh-gcm:+'
implementation 'com.android.support:support-v4:+'
implementation 'com.google.android.gms:play-services-gcm:+'

Replace "+" with the current version of Pushwoosh, Android Support and Google Play Services libraries.

3. To finish integration, please follow steps 7 and 8 of our FCM integration guide.

Using two GCMListenerService's

It is possible to use two different SDK's both using GCM in your app. Usually one runs into the following problem: GcmReceiver launches only one service, as a result only one subclassed service will receive push notifications and events. This would be either the one which appears the first in AndroidManifest.xml or the one with the higher priority (as outlined here: https://developer.android.com/guide/topics/manifest/intent-filter-element.html)

To solve this problem create master GcmListenerService and proxy events to other services. See the example:

public class GCMListenerRouterService extends GcmListenerService {
    private void dispatchMessage(String component, Bundle data) {
        Intent intent = new Intent();
        intent.putExtras(data);
        intent.setAction("com.google.android.c2dm.intent.RECEIVE");
        intent.setComponent(new ComponentName(getPackageName(), component));
  
        GcmReceiver.startWakefulService(getApplicationContext(), intent);
    }
  
    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.i("PushwooshTest", "Gcm router service received message: " + (data != null ? data.toString() : "<null>") + " from: " + from);
  
        // Base GCM listener service removes this extra before calling onMessageReceived
        // Need to set it again to pass intent to another service
        data.putString("from", from);
  
        if (TextUtils.equals(from, getString(R.string.PUSHWOOSH_PROJECT_ID))) {
            dispatchMessage(PushGcmIntentService.class.getName(), data);
        }
        else if (TextUtils.equals(from, getString(R.string.PRIVATE_PROJECT_ID))) {
            dispatchMessage(PrivateGCMListenerService.class.getName(), data);
        }

    }
}

Register this receiver with the highest priority:

<service
    android:name=".GCMListenerRouterService"
    android:enabled="true"
    android:exported="false" >
    <intent-filter android:priority="100" >
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

Use the following code to handle push token changes:

Subscription<RegistrationSuccessEvent> registrationSuccessEventSubscription = EventBus.subscribe(RegistrationSuccessEvent.class, new EventListener<RegistrationSuccessEvent>() {
    @Override
    public void onReceive(RegistrationSuccessEvent event) {
        String token = event.getData();
        //send the push token to the other service
    }
});

GCM Integration (legacy)

This guide addresses the GCM integration