Android SDK Integration

Quick Steps Guidelines

Step 1: Select a Platform

  • Navigate to Netmera Panel > Developers > Netmera Onboarding and select Android.

Step 2: Create A Firebase Cloud Messaging Configuration

  • Create a project in the Firebase Developers Console.

  • Download and upload the New Private Key file to Netmera Onboarding Step 2.

Step 3: Select A Target SDK

  • Choose the appropriate SDK version for your application.

Step 4: Integrate and Initialize Netmera SDK

  • Add necessary configurations to your build.gradle.

  • Initialize Netmera in the onCreate() of your android.app.Application.

Android 13 Push Notification Permissions

  • Ensure Android 13 push notification permissions are requested.

Android SDK integration completed 👏

Step 1: Select a Platform

Navigate to Netmera Panel > Developers > Netmera Onboarding and choose Android.

Click on Android > Start to proceed.

Step 2: Create a Firebase Configuration

Netmera uses Firebase Cloud Messaging (FCM) to deliver push messages to the Android devices. Therefore, you should create and configure a project on Firebase Developers Console.

  • At the end of the Firebase configuration, you will have a New Private Key folder of your project. That folder must be uploaded to Developers > Netmera Onboarding > Android > Step 2: Create A Firebase Configuration > FCM Service Account Key section on Netmera Panel.

Firebase Cloud Messaging Configuration

Firebase Cloud Messaging - Generate New Private Key

Upload FCM Service Account Key on Netmera Panel

Step 3: Select a Target SDK

Select the appropriate SDK for your application development framework from the following options: Native Android, Flutter, React Native, Unity. Note: The subsequent instructions on this page are specific to the Native Android SDK.

Step 4: Integrate and Initialize Android SDK

⚠️ Warning: Do not use the API key from your test panel when setting up or deploying your production app. Each panel has its own unique API key. Even if you log in with a different account, this encrypted key stays the same. Using the wrong key can result in data being sent to the incorrect environment, causing potential issues or errors in your application.

Integrate Netmera SDK

The Netmera Android SDK is conveniently available through a Maven repository. To integrate it into your project, you only need to add the following configurations to your build.gradle file.

AndroidManifest and other resource configurations are handled automatically by the Android Gradle build tool.

Complete the following three steps below to integrate Netmera SDK.

Project Gradle - App Gradle - Setting Gradle:

plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
    id 'com.google.gms.google-services' version '4.3.14' apply false
}

If you get the following error:

“Problem occurred evaluating root project. Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle’ issue while building”

Go to setting.gradle and remove dependencyResolutionManagement block.

More info can be found here: https://stackoverflow.com/a/69197871

Initialize Netmera SDK

Add the following line of code in onCreate() method of your class that extends **android.app.Application**.

If you don't have such a class, you should create it and add it to your AndroidManifest file via android:name attribute of the application tag.

fun onCreate() {
super.onCreate()
        val configBuilder: NetmeraConfiguration.Builder = Builder()
        configBuilder.baseUrl(baseUrl).apiKey(apiKey)
            .firebaseSenderId(PropertiesUtil.gcmSenderId)
            .huaweiSenderId(PropertiesUtil.hmsSenderId) //==> Init example if you are going to call Huawei services;
            .logging(true) //==>  Allows netmera logs to appear in logcat
            .disableSerializeRule(false)  //it should be only set to false or it should not be set at all
            .nmInAppMessageActionCallbacks(NGInAppMessageActionCallbacks())
            .nmPushActionCallbacks(NGPushActionCallbacks())
            .nmWebWidgetCallbacks(NGWebWidgetCallbacks())
        Netmera.init(configBuilder.build(this))
}
If the auto-start doesn't work:

Once these enhancements have been implemented, simply calling our initialization method and creating NetmeraConfigurations will be sufficient to send requests to the SDK, which will automatically handle the rest.

The reason we emphasize the automation aspect is because we leverage the AutoStartup framework provided by Android, which initiates before the initialization method is invoked.

However, in the event that this feature is disabled in your application, you can integrate Netmera using the example code block provided below. Unless AutoStartup is disabled, there is no need to utilize Initializer classes.

Kotlin:

fun onCreate() {
super.onCreate()
	NMInitializer.initializeComponents(this)
        AppInitializer.getInstance(this).initializeComponent(NMFCMProviderInitializer::class.java)
        AppInitializer.getInstance(this)
            .initializeComponent(NMHMSProviderInitializer::class.java) //==> Init example if you are going to call Huawei services;
        val configBuilder: NetmeraConfiguration.Builder = Builder()
        configBuilder.baseUrl(baseUrl).apiKey(apiKey)
            .firebaseSenderId(PropertiesUtil.gcmSenderId)
            .huaweiSenderId(PropertiesUtil.hmsSenderId) //==> Init example if you are going to call Huawei services;
            .logging(true) //==>  Allows netmera logs to appear in logcat
            .disableSerializeRule(false)  //it should be only set to false or it should not be set at all
            .nmInAppMessageActionCallbacks(NGInAppMessageActionCallbacks())
            .nmPushActionCallbacks(NGPushActionCallbacks())
            .nmWebWidgetCallbacks(NGWebWidgetCallbacks())
        Netmera.init(configBuilder.build(this))
}

Java:

@Override public void onCreate() {
    super.onCreate();
NMInitializer.initializeComponents(this);
AppInitializer.getInstance(this).initializeComponent(NMFCMProviderInitializer.class);
AppInitializer.getInstance(this).initializeComponent(NMHMSProviderInitializer.class);  //==> Init example if you are going to call Huawei services;
NetmeraConfiguration.Builder configBuilder = new NetmeraConfiguration.Builder();
configBuilder.baseUrl(baseUrl).apiKey(apiKey)
    .firebaseSenderId(PropertiesUtil.gcmSenderId)
    .huaweiSenderId(PropertiesUtil.hmsSenderId) //==> Init example if you are going to call Huawei services;
    .logging(true)  //==>  Allows netmera logs to appear in logcat
    .disableSerializeRule(false)
    .nmInAppMessageActionCallbacks(new NGInAppMessageActionCallbacks())
    .nmPushActionCallbacks(new NGPushActionCallbacks())
    .nmWebWidgetCallbacks(new NGWebWidgetCallbacks());
Netmera.init(configBuilder.build(this));

}

If your application on-premise, add this code:

configBuilder.baseUrl(baseUrl).apiKey(apiKey)

If your application is not on-premise, add this code:

configBuilder.apiKey(apiKey)

Important Note / Init Methods:

You should add init methods in main thread. They should not be added in the background.

Init Callback (optional)

This use is optional. onSuccess and onFailure callbacks are added to the Init method in Android Core Update version 3.11.3. Example usage is as followed,

For further information you may visit the following page Push Callbacks.

Netmera.init(configBuilder.build(this), new NMInitSessionListener() {
    @Override
    public void onSuccess() {
        Log.i("NetmeraApp", "Session init completed");
    }

    @Override
    public void onFailure(String error) {
        Log.e("NetmeraApp", "Error occurred when trying to init session with Netmera. Error::"+error );
    }
});

Enable Logging (optional)

You can also enable logging with the code below. Add the following line of code in

onCreate() method of your class that extends ** android.app.Application**

.logging(true)

Android 13 Push Notification Permissions

Applications targeting API Level 33 and above are empowered to request push notification permissions from users as needed. This decision is in the hands of the Android operating system.

For all applications with a target level below API Level 33, when the application is running on a device with Android 13 or higher, the operating system will automatically prompt the user for these permissions during the initial creation of notification channels.

Since Netmera SDK creates notification channels after receiving the initialization response, and we advise you to call the init method at the application level, this permission request will be triggered automatically as soon as the application is launched. However, this automatic request applies only to applications targeting Android API Level 32 and below, and it takes effect on devices running Android 13 or higher.

On devices running Android 12 and earlier, the system already behaves as if this permission has been granted by default.

Push Permission Methods in Netmera SDK

For applications targeting API 33, here's how you can use Netmera SDK when requesting push permission. At this stage, there are two important methods in Netmera SDK.

fun requestNotificationPermissions(activity: Activity?)

This method triggers the necessary methods to send a request for permissions to the system at any time.

Push Enable/Disable:

  1. The users triggers the requestNotificationPermissions() method, the user grants permission (push enable request is sent).

After the customer receives a denial, they send the same request again (it is recommended by Google not to resend it immediately after receiving a denial), and the SDK opens the Notification settings that are specific to that application on the operating system. The user grants notification permission from the settings and returns to the application (push enable request is sent).

  1. The users triggers the requestNotificationPermissions() method, the user denies permission (push disable request is sent).

After the customer receives a denial, they send the same request again (it is recommended by Google not to resend it immediately after receiving a denial), and the SDK opens the Notification settings that are specific to that application on the operating system. The user cancels (presses the back button) (push disable request is sent).

This method allows checking whether the necessary permissions for the application have been obtained. If the required permissions have been obtained from the user by the system, the method will return true, otherwise, it will return false.

fun areNotificationsEnabled(): Boolean

Email Preferences (optional)

In version 3.9.18, a feature related to Email Preferences has been introduced. By invoking the following method, you can request email permission from users.

fun setEmailPermission(isAllowed: Boolean) 

To clarify, if you execute the following code:

Netmera.setEmailPermission(false)

It means that email permission is revoked on your end.

Conversely, if you execute:

Netmera.setEmailPermission(true)

It signifies that permission for sending emails is granted.

Firebase Messaging Service Class (optional)

If you are incorporating Netmera's push notifications alongside another Firebase Cloud Messaging (FCM) integration, here's what you need to do:

  1. In your Firebase Messaging Service class, consider calling Netmera's onNetmeraPushMessageReceived method in your own onMessageReceived method.

  2. When your FCM token is renewed, remember to call the onNetmeraNewToken method to ensure the new token is forwarded to Netmera. You can do this by calling Netmera.onNetmeraNewToken(token).

By following these steps, you can seamlessly integrate Netmera's push notifications into your existing FCM setup.

class PushMessaging : FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        if (Netmera.isNetmeraRemoteMessage(remoteMessage)) {
            Netmera.onNetmeraPushMessageReceived(remoteMessage)
        } else {
            // another operations
        }
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Netmera.onNetmeraNewToken(token)
    }
}

Important Note for Obfuscation:

If you are obfuscating your code, you don't need to add any specific rules for Netmera. Netmera's functionality remains intact even with code obfuscation.

Android SDK integration completed 👍

Android SDK integration has been successfully completed, and your devices are now ready to receive the following types of push notifications sent via the Netmera Dashboard:

  1. Standard Push Notifications

  2. Interactive Push Notifications

  3. Push Notifications with Deeplinks (Android Deeplink)

Warning During App Store Submission:

During your application's store submission, you may encounter the com.google.android.gms.permission.AD_ID alert. In this case, after proceeding with 'yes,' on the subsequent page, the 'analytics' option should also be selected. After this selection, there is no need for any additional additions to the manifest; merging the manifest is sufficient.

Last updated