Tutorial: Push notifications

Android Push notifications

Webcom SDK for Android makes it possible to subscribe to some data changes on the Webcom realtime database and to be notified of changes through the push notification channel of mobile devices. This is especially interesting to wake up an application when it has switched to background or when the user has closed or stopped it on her·his device.

In the Android universe, this feature relies on either:

  • the Firebase Cloud Messaging (or FCM) system for Google-certified Android devices,
  • the Huawei Mobile Services Push Kit system for non Google-certified devices.

Using Firebase Cloud Messaging

In addition to the Firebase developer console settings (see the “Mobile Push Notifications” chapter), some actions are required on your Android application itself:

  1. In the top-level build.gradle file of your Android project, add the dependency to the Google Services plugin:
buildscript {
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.3.10'
        // ...
    }
}
  1. In the concerned module of your Android project, provision the google-services.json file (to export from the Firebase console) into the src directory.
  2. In the build.gradle file of the module:
    • Enable the Google Services plugin,
    • Add the dependency to the FCM libraries:
plugins {
    // ...
    id 'com.google.gms.google-services'
}
// ...
dependencies {
    // ...
    implementation 'com.google.firebase:firebase-bom:28.4.2'
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    // ...
}
  1. Add a Firebase Messaging Service to your AndroidManifest.xml file, within the <application> tag:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <!-- ... -->
    <application ...>
        <service android:name=".YourClassThatReceivesFcmMessages" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    </application>
    ...
</manifest>
  1. Follow the development guides given in the “Mobile Push Notifications” chapter.

Using Huawei Push Kit

(this section has not been fully tested, it is written based on Huawei official documentation)

In addition to the Huawei developer console (or AppGallery Connect) settings (see the “Mobile Push Notifications” chapter), some actions are required on your Android application itself:

  1. In the top-level build.gradle file of your Android project, add the maven repository for HMS Core SDK to the buildscript.repositories and allprojects.repositories sections and add the dependency to the AppGallery Connect plugin:
buildscript {
    repositories {
        // ...
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        // ...
        classpath 'com.huawei.agconnect:agcp:1.6.0.300'
        // ...
    }
}
allprojects {
    repositories {
        // ...
        maven {url 'https://developer.huawei.com/repo/'}
    }
}
  1. In the concerned module of your Android project, provision the agconnect-services.json file (to export from the Huawei AppGallery Connect console) into the src directory.
  2. In the build.gradle file of the module:
plugins {
    // ...
    id 'com.huawei.agconnect'
}
// ...
dependencies {
    // ...
    implementation 'com.huawei.hms:push:5.3.0.304'
    // ...
}
// ...
android {
    signingConfigs {
        config {
            keyAlias 'xxx'
            keyPassword 'xxxx'
            storeFile file('xxx.jks')
            storePassword 'xxxx'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.config
            // ...
        }
        release {
            signingConfig signingConfigs.config
            // ...
        }
    }
}
  1. Add an HMS Messaging Service to your AndroidManifest.xml file, within the <application> tag:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <!-- ... -->
    <application ...>
        <service android:name=".YourClassThatReceivesHmsMessages" android:exported="false">
            <intent-filter>
                <action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    </application>
    ...
</manifest>
  1. Follow the development guides given in the “Mobile Push Notifications” chapter.