Android SDK Documentation¶
Mondiad offers an Android SDK that enables you to incorporate ad monetization into your Android application. Sign up your app at Mondiad Members as a Publisher to obtain your app key and begin monetizing your app. Currently, the Android SDK supports only push notifications.
Setting up Push Notifications¶
Push notifications utilize the industry-standard Firebase Cloud Messaging (FCM) to deliver ads to your app. This requires configuration in the Mondiad dashboard, the Firebase console, and within your app. Let's get started!
Step 1: Register your app¶
To get your app key, register as a Publisher at Mondiad Members. After registering your app in the Mondiad dashboard, create a new Push Notification Ad Zone to generate a code snippet
Step 2: Configuring your Android app¶
2.1. Add required libraries to your project¶
In dependencies section of your app's build.gradle file, add the following dependencies:
dependencies {
// your other dependencies here
implementation("com.google.firebase:firebase-core:21.1.1")
implementation("com.google.firebase:firebase-messaging:24.1.0")
implementation("com.mndiad:android-sdk:1.0.0")
}
plugins {
// your other plugins here
id("com.google.gms.google-services") version "4.4.2"
}
2.2. Initialize the Mondiad SDK¶
Ensure you have a custom Application
class defined. If you don’t have one yet,
create a class that extends Application. If you already have an Application class,
simply add the initialization code there.
The minimal Application
class should look like this:
import android.app.Application
import com.mndiad.android.sdk.Mnd
class App : Application() {
override fun onCreate() {
super.onCreate()
// Initialize Mondiad SDK
Mnd.push.init(this, $zoneId) // Replace $zoneId with your actual zone ID
}
}
This code snippet can be obtained from the Mondiad dashboard after creating a new Ad Zone in step 1.
Also, if you don't have an Application
class defined in your project, you need to declare
it in your AndroidManifest.xml
file:
<application
android:name=".App"
...>
<!-- other activities, services, etc. -->
</application>
2.3. Obtaining google-services.json file¶
Create and Register your app in the Firebase console. Download the google-services.json
file and
put it in the app
directory of your project. More information on how to do this can be found in the
Firebase documentation.
2.4. Handling push notifications¶
To handle push notifications, you need to create a service that extends FirebaseMessagingService
:
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.mndiad.android.sdk.Mnd
class MessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
Mnd.push.onNewToken(token)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Mnd.push.onMessageReceived(message)
}
}
The service should be registered in the AndroidManifest.xml
file inside the <application>
tag:
<application ...>
<service android:name=".MessagingService"
android:exported="false">
<intent-filter android:priority="999">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Step 3: Provide Your Firebase Credentials to Mondiad¶
To send push notifications to your app, Mondiad needs your Firebase credentials.
You can provide one of the following types of credentials:
- Firebase Admin SDK credentials
- Firebase Cloud Messaging Server Key
The Firebase Admin SDK credentials offer more permissions and are easier to obtain. However, the Firebase Cloud Messaging Server Key is more secure due to its limited permissions. Choose the credential type that best suits your needs and provide it to Mondiad on the Ad Zone configuration page.
3.1. Firebase Admin SDK credentials¶
To obtain Firebase Admin SDK credentials, follow these steps:
- Log in to the Firebase console.
- Go to Project Settings.
- Click on the Service Accounts tab and then on the Generate New Private Key button.
- Save the generated JSON file and upload it to the Mondiad dashboard.
3.2. Firebase Cloud Messaging Server Key¶
In case if you want to limit the permissions of the credentials, you can use the Firebase Cloud Messaging Server Key.
- Log in to the Firebase console.
- Go to Project Settings.
- Click on the Cloud Messaging tab then on Manage Service Accounts.
- Click on the Create Service Account button.
- Fill in the required fields and click on the Create And Continue button.
- Choose the role Firebase Cloud Messaging API Admin and click on the Done button.
- Select the created service account and click on the Manage Keys button.
- Create new JSON key and save it.
- Upload the JSON file to the Mondiad dashboard.
Additional Configuration Options¶
The Mnd.push.init()
method can take an optional PushOptions
object as its last parameter.
You can create this object using the PushOptions.Builder
class.
val options = PushOptions.Builder().apply {
logLevel = Log.INFO
singleNotificationMode = false
subscribeOnStartup = false
}.build()
Mnd.push.init(this, $zoneId, options)
PushOptions options = new PushOptions.Builder()
.logLevel(Log.INFO)
.singleNotificationMode(false)
.subscribeOnStartup(false)
.build();
Mnd.push.init(this, $zoneId, options);
Available options:
- log level: Determines the verbosity of the SDK's logging. The minimum value is
Log.INFO
. To suppress all logs, useLog.ASSERT
or higher. This option does not affect the Firebase SDK's logging. The default value isLog.INFO
. - single notification mode: If set to
true
, only the most recent notification will be shown at a time. If a new notification arrives while the previous one is still displayed, the new notification will replace the old one. If set tofalse
, all notifications will be displayed. The default value isfalse
. - subscribe on startup: If set to
true
, the SDK will automatically force the user to subscribe to push notifications on the first app launch. If set tofalse
, the user will be asked to subscribe only after a call toMnd.push.tryToSubscribe()
. The default value istrue
.
Delayed Initialization¶
In some cases, you may want to delay asking the user for push notification permissions. You can do
this by calling Mnd.push.init()
with the subscribeOnStartup
option set to false
.
Then, when you are ready to ask the user for permissions, call Mnd.push.tryToSubscribe()
, for example:
fun subscribe() {
Mnd.push.tryToSubscribe { success ->
if (success) {
// User has subscribed
} else {
// User has declined
// For your convenience, you can open the system notifications dialog for the user:
Mnd.push.openNotificationsSettings()
}
}
}
void subscribe() {
Mnd.push.tryToSubscribe(success -> {
if (success) {
// User has subscribed
} else {
// User has declined
// For your convenience, you can open the system notifications dialog for the user:
Mnd.push.openNotificationsSettings();
}
return null;
});
}
The callback will be called in the main thread and will indicate whether the user has subscribed
or declined. Also, you can direct the user to the system notification settings by calling
Mnd.push.openNotificationsSettings()
when the user has declined.