embedded_fcm_distributor

Embed a FCM distributor as a fallback if user don't have another distributor. Uses Google proprietary blobs.

This library requires Android 5.0 or higher.

Import the library

Add the dependency to the module build.gradle. Replace {VERSION} with the latest version.

dependencies {
// ...
implementation 'org.unifiedpush.android:embedded-fcm-distributor:{VERSION}' {
exclude group: 'com.google.firebase', module: 'firebase-core'
exclude group: 'com.google.firebase', module: 'firebase-analytics'
exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
}
}

Setup Google Services

Add google-services to the build dependencies, in the root build.gradle. Replace {VERSION} with the latest version.

classpath 'com.google.gms:google-services:{VERSION}'

Apply google-services plugin for your fcm flavor in your module build.gradle. (You may need to edit the pattern)

def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
String flavor

Pattern pattern

if( tskReqStr.contains( "assemble" ) )
pattern = Pattern.compile("assemble(\\w+)")
else
pattern = Pattern.compile("generate(\\w+)")

Matcher matcher = pattern.matcher( tskReqStr )

if( matcher.find() ) {
flavor = matcher.group(1).toLowerCase()
}
else
{
println "NO MATCH FOUND"
return ""
}

pattern = Pattern.compile("^fcm.*");
matcher = pattern.matcher(flavor);

if( matcher.matches() ) {
return "fcm"
} else {
return "main"
}
}

println("Flavor: ${getCurrentFlavor()}")
if ( getCurrentFlavor() == "fcm" ){
apply plugin: 'com.google.gms.google-services'
}

Download google-services.json from the firebase console, and add it to the module directory.

Expose a receiver

You need to expose a Receiver that extend EmbeddedDistributorReceiver and you must override getEndpoint to return the address of your FCM rewrite-proxy.

class EmbeddedDistributor: EmbeddedDistributorReceiver() {
override fun getEndpoint(context: Context, fcmToken: String, instance: String): String {
// This returns the endpoint of your FCM Rewrite-Proxy
return "https://<your.domain.tld>/FCM?v2&instance=$instance&token=$token"
}
}

Edit your manifest

The receiver has to be exposed in the AndroidManifest.xml in order to receive the UnifiedPush messages.

<receiver android:enabled="true"  android:name=".EmbeddedDistributor" android:exported="false">
<intent-filter>
<action android:name="org.unifiedpush.android.distributor.feature.BYTES_MESSAGE"/>
<action android:name="org.unifiedpush.android.distributor.REGISTER"/>
<action android:name="org.unifiedpush.android.distributor.UNREGISTER"/>
</intent-filter>
</receiver>

Packages