Android SDK Integration

Quick Steps Guidelines

Step 1: Create A Firebase Cloud Messaging Configuration

Create and configure a FCM project on Firebase Developers Console

Step 2: Integrate SDK

Add configurations below to your build.gradle file.

Step 3: Initialize Netmera

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

Step 4: Android 13 Push Notification Permissions

Request push notification authorization from users.

Android SDK integration completed πŸ‘

Step 1: Create A Firebase Cloud Messaging Configuration

  1. 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.

  2. 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.

Sender ID Must Not Change:

If you are migrating from GCM to FCM and do not have a Firebase Console account, it's crucial to keep your sender ID unchanged. The sender ID is responsible for token assignment, and any changes in the sender ID will render existing tokens unusable. You can find more information about migrating from GCM to FCM in the following link: Migration Guide.

Step 2: Integrate Netmera Android 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 / App / 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

Step 3: Initialize Netmera

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));

}

Please make sure you get the API key value from the relevant panel

YOUR_SDK_API_KEY: You can get api key from Developers > API > SDK API Key from your web panel.

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)

Step 4: 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