Comment on page
Android SDK Integration
Quick Steps Guidelines
Step 1: Create A Firebase Cloud Messaging Configuration
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
👏
- 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 configuration, you will see values of Server API Key and Sender ID of your project. Those values must be added to the Netmera Dashboard and Sender ID must be used in client during initialization.
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.
Step 1. Project Gradle
Step 2. App Gradle
Step 3. 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
}
You should add Netmera dependency to your dependencies section in your
build.gradle(:app)
plugins{
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
dependencies {
implementation 'com.netmera:nmcore:3.10.4'
implementation 'com.netmera:nmfcm:3.10.2'
implementation 'com.netmera:nmhms:3.10.1'
}
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url "https://release.netmera.com/release/android" }
}
}
rootProject.name = "NetmeraApp"
include ':app'
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.
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.
Kotlin
Java
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)
.nmInAppMessageActionCallbacks(NGInAppMessageActionCallbacks())
.nmPushActionCallbacks(NGPushActionCallbacks())
.nmWebWidgetCallbacks(NGWebWidgetCallbacks())
Netmera.init(configBuilder.build(this))
}
@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.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**
Kotlin
Java
.logging(true)
Netmera.logging(true);
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.NMInitializer.initializeComponents(this);
AppInitializer.getInstance(this).initializeComponent(NMFCMProviderInitializer.class);
AppInitializer.getInstance(this).initializeComponent(NMHMSProviderInitializer.class);
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.
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.
Kotlin
Java
fun areNotificationsEnabled(): Boolean
public static boolean areNotificationsEnabled()
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.
Kotlin
Java
fun requestNotificationPermissions(activity: Activity?)
public static void requestNotificationPermissions(Activity activity)
This method triggers the necessary methods to send a request for permissions to the system at any time.
- 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).
- 2.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).
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.
Kotlin
Java
fun setEmailPermission(isAllowed: Boolean)
public static void setEmailPermission(boolean isAllowed);
To clarify, if you execute the following code:
Kotlin
Java
Netmera.setEmailPermission(false)
Netmera.setEmailPermission(false);
It means that email permission is revoked on your end.
Conversely, if you execute:
Kotlin
Java
Netmera.setEmailPermission(true)
Netmera.setEmailPermission(true);
It signifies that permission for sending emails is granted.
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 ownonMessageReceived
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 callingNetmera.onNetmeraNewToken(token)
.
By following these steps, you can seamlessly integrate Netmera's push notifications into your existing FCM setup.
Kotlin
Java
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)
}
}
public class PushMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (Netmera.isNetmeraRemoteMessage(remoteMessage)) {
Netmera.onNetmeraPushMessageReceived(remoteMessage);
} else {
//another operations
}
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Netmera.onNetmeraNewToken(token);
}
}
If You Don't Use Location Permission in the App:
If your app does not request location permission from the user, follow these steps to manage permissions in your manifest file:
- 1.Open your AndroidManifest.xml file.
- 2.If you are not requesting location permissions, add the following code block to your manifest file to remove location-related permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" tools:node="remove" />
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.
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
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 (If you've configured and published them using the Dashboard)
- 3.Push Notifications with Web View Content
- 4.Push Notifications with Deeplinks (If your application supports URL Scheme-based deeplinks and you've configured the application's URL Scheme in the Dashboard.)
Last modified 5d ago