Support Google and Huawei Push Notification

Stavros Georgiou
2 min readJan 26, 2021

Google Play and other Google GMS functionality are no longer available on new Huawei Android devices making an enormous amount of apps incompatible with them. Although the API of both frameworks is very alike if not identical, they rely on different dependencies making it impossible to maintain an identical code for Google Play and Huawei AppStore.

Unwillingly to maintain two separate projects or branches for the same app, I came to the following solution which minimizes the code divergence by using two different app flavors while encapsulating these specific traits on flavored packages making the delivery of these variants trivial.

Create two new flavors

On our app-level build.gradle add a new flavor dimension named “platform” . We need to declare then two new flavors under productFlavors “google” and “huawei” and set the dimension to “platform” for both flavors.

Push Notification is a service and we need to declare it in the Android Manifest. You can see that i have use ${providerPrefix}. This way I can change between “com.google.firebase” and “com.huawei.push.action” MESSAGING_EVENT.

<service
android:name=".CustomMessagingService"
android:exported="false">
<intent-filter>
<action android:name="${providerPrefix}.MESSAGING_EVENT" />
</intent-filter>
</service>

On our project-level build.gradle we declare huawei repository and the dependencies for huawei plugin

Create CustomMessagingService Class

Create a path to keep the flavor-specific code, for example you should create:

app/src/huawei/java/com/company/notification/”

app/src/google/java/com/company/notification/”

Create the same class for each path, for example “CustomMessagingService” which extends Huawei’s HmsMessageService for huawei flavor and Google’s FirebaseMessagingService for google flavor.

After both paths have been created, the packages will be shown on the File Explorer but mind that only the packages from the selected flavor are shown into the Android File Explorer. To show the package and content of the other flavor, you have to change the active flavor from the Build Variants tab.

End of Story

This will be useful to maintain the core of your app agnostic to third-party libraries API’s that you may want to change or switch without the need to perform a lot of code changes. This approach may be useful to implement different libraries analogs according to the distribution platforms like Google Map-Huawei MAP Kit.

I hope you enjoyed this article and you found it useful. If so, do give it a thumbs up, comment on it and share with with your friends.

https://twitter.com/stavris8894

--

--