Enabling Huawei Push Notifications for New Apps

This article covers the process of enabling Huawei push notifications and registration of a new app. If you have already registered the app, refer to this article for the process of enabling Huawei push notifications for a registered app.

You enable Huawei push notifications for an existing or a new push channel on the Channels page.

Before you continue with the setup, verify that you have the following:

An Android app with minSdk no earlier than 21 (Android 5) and targetSdk no later than 31 (Android 12)
The Sender ID and Server Key parameters from Firebase
The Client Id and Client Secret parameters from Huawei AppGallery
The google-services.json file from Firebase
The agconnect-services.json file from Huawei AppGallery
A device with Android 5.0 or higher
A current version of Android Studio

Note that the process that follows assumes that you have already completed the steps described in the Adding Push Channels article.

Step 1. Enter App Data from Firebase and Huawei

Enable Firebase Cloud messaging for the app even if you are only planning to distribute it via Huawei AppGallery — this will increase the percentage of push delivery.

  1. Register the app in Firebase (if you haven’t done it yet).
  2. Obtain the Sender ID and Server Key parameters in Firebase.
  3. On the Settings > Channels > Setting Android Application page in the edna Platform, enter the name of the app, the Firebase Sender ID, and the Firebase API Key (Sender ID and Server Key respectively).

  4. Select the Add a HUAWEI app check box.
  5. Register the app in Huawei AppGallery (if you haven’t registered it yet) and obtain the Client ID and Client Secret parameters from the console.
  6. Enter the parameters in the respective fields.

  7. Click Save and Continue.

Step 2. Add Dependencies to build.gradle File

  1. Add dependencies to the project’s build.gradle file — specify the edna repository address and add Google and Huawei services to the list of dependencies.
  buildscript {  
    repositories {
        //...
        maven { url 'http://developer.huawei.com/repo/' }
    }
      //...
    dependencies {
        //...
        classpath "com.google.gms:google-services:4.3.10"
        classpath "com.huawei.agconnect:agcp:1.6.0.300"
    }
    //...
  allprojects {
    repositories {
       //...
        maven { url 'https://maven.edna.ru/repository/maven-public'}
        maven { url 'http://developer.huawei.com/repo/' }
    }
}
  1. To the build.gradle file of the app, apply the plugin, specify the app unique access key for the edna push services, and add edna library to the list of dependencies.
      apply plugin: 'com.google.gms.google-services'
      apply plugin: 'com.huawei.agconnect'
      defaultConfig {
          //...
          // Unique application key
          manifestPlaceholders = [edna_app_id:
          "your access key to edna push services"]
      }

      dependencies {
         //...
         implementation "com.edna.android:push-x:1.2.2"
         implementation "com.edna.android:push-x-huawei:1.2.2"
      }
  1. Add the following rules to the proguard-rules.pro file:
    
    -keep class com.edna.android.push_lite.**{*;}
    -keep class com.hianalytics.android.**{*;}
    -keep class com.huawei.updatesdk.**{*;}
    -keep class com.huawei.hms.**{*;}
  1. To the root folder of your app, add the google-services.json file you obtained in Firebase for your app and the agconnect-services.json file you obtained in Huawei AppGallery.

Step 3. Add Code for Processing Notification Taps

If a client taps a notification or its action button, the app launches. The edna library processes the tap-through data and passes Intent to the app’s start Activity. You need to process the data received from the edna library in the app’s onCreate and onNewIntent methods. The edna library passes an action string you specified in the broadcast to Intent.

Following is an example of the code for processing a client’s notification tap-through:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //...
        intent?.extras?.getParcelable<PushAction>(PUSH_ACTION_KEY)?.let {
        // Client navigation based on the passed string with PUSH_ACTION_KEY = "action"
            redirectUser(it.action)
        }
        //...
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        //...
        intent?.extras?.getParcelable<PushAction>(PUSH_ACTION_KEY)?.let {
        // Client navigation based on the passed string with PUSH_ACTION_KEY = "action"
            redirectUser(it.action)
        }
        //...
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PushAction extraAction = getIntent().getExtras().getParcelable(PUSH_ACTION_KEY);
    // Client navigation based on the passed string with PUSH_ACTION_KEY = "action"
        redirectUser(extraAction);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        PushAction extraAction = getIntent().getExtras().getParcelable(PUSH_ACTION_KEY);
    // Client navigation based on the passed string with PUSH_ACTION_KEY = "action"
        redirectUser(extraAction);
    }
}

Step 4. Support Registration of Client Devices in edna

  1. Select the type of how the device is registered on the platform.

    A client’s device appears in edna after it is registered. Two methods – automatic or manual – are possible. For now, only the automatic registration is available, but the manual type will be added soon as well.
    • With automatic registration, edna library registers the device in edna by itself. This method is simpler, it does not require you to manage the registration process, but it is not recommended for sending confidential data.
    • Once the manual type is available, your backend will be able to register the client’s device in edna using a specific API method.
  2. To register a client in edna, call the login library method specifying ID and SubscriberIdType of the client. Call the method only after the client is successfully authorized in your app.
PushX.login("79000000000", SubscriberIdType.PHONE_NUMBER)
PushX.login("79000000000", SubscriberIdType.PHONE_NUMBER.INSTANCE)

The following types of SubscriberIdType are available:

Phone number (PhoneNumber)
Email address (Email)
IDs in Facebook, Telegram, Google, Apple, Yandex (FacebookId, TelegramId, GoogleId, AppleId, YandexId)
Internal user IDs (ExtUserId, CookieId)
UTM tag (UTM)

  1. When the user is deauthorizing, call the logout library method to log out the device from edna:
PushX.logout()

Step 5. Compile and Launch App

  1. Connect the device to the Internet to check the integration.
  2. Compile and launch the app.
  3. After the app is launched, on the Settings > Channels > Setting Android Application page in the edna Platform, click Save and continue. A page with step 6 will open, you need to wait until the registration process is completed.

Step 6. Check Device Connection and Test Receiving Push Notifications

In this step, edna helps you verify the integration has been completed correctly. If it has, you will see the name of your test device and the button to send a test push notification. Note that if you launched the app on several devices, you will only see the last connected one.

  1. Click Test push notifications.
  2. Verify the push notification is delivered and displayed on your test device.
  3. If yes, confirm receiving the push by clicking Yes.

All done! You can now create a cascade and set up a broadcast using this push channel.

Enabling Android Push Notifications
Next Article Enabling Huawei Push Notifications for Registered Apps