push setup - optimove-tech/Optimove-SDK-Android GitHub Wiki

Configuration & Setup (FCM)

For implementing our mobile push channel, please ensure that the Basic Setup steps have been completed to add the SDK to your app. Once completed, follow the instructions below to finish the required setup for mobile push for Android.

Set up a Firebase Project & enable FCM

In order to enable push notifications for Android with Optimove, you'll need to set up an FCM project and configure push for your app.

Optimove requires a service account to execute campaigns for your FCM project. This service account should contain a custom role with just one permission - cloudmessaging.messages.create (you can read about custom roles here, about this specific permission here)

Follow these steps to generate this key:

  1. Go to your Google Cloud Platform console
  2. Navigate to IAM & Admin
  3. Navigate to Roles
  4. Click on "Create Role" at the top
  5. Fill the fields (As a name you can choose something like 'Optimove FCM')
  6. Click on "Add Permissions"
  7. Search for cloudmessaging.messages.create and choose it
  8. Click create
  9. Navigate to Service Accounts
  10. Click on "Create Service Account" at the top
  11. Select the custom role you created in 8
  12. Click done
  13. Click on the service account you generated
  14. Generate a key to this service account

If you have multiple App projects (for example, one for production and one for staging), you must perform this step for each project.

Please note it is the Google Service Account private key JSON file that needs to be uploaded to your mobile UI, not the Google Services JSON file you include in your app project.

Configure Mobile Push Channel

Access your Optimove dashboard and select 'Settings' then under the 'OptiMobile' section, select 'Mobile Push Config'

Click the cog icon in the row for Android and in the popup when prompted enter the credentials for FCM.

Integrate SDK components with your Android project

After configuring the Mobile Push Channel & FCM, simply follow the SDK integration steps below.

To begin, ensure you have added the Firebase components to your app as shown below.

In the root build.gradle, ensure the Google repository is enabled and the google-services plugin is on the classpath:

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.3.10' // google-services plugin
    }
}

allprojects {
    // ...
    repositories {
        google() // Google's Maven repository
        // ...
    }
}

Next, in your app module's build.gradle add FCM dependency and apply the google-services plugin. Optimove supports [19.0.0, 22.99.99] Firebase Messaging version range, which corresponds to [20.0.0, 28.99.99] Bill of Materials version range.

apply plugin: 'com.android.application'

dependencies {
    ...
     implementation 'com.optimove.android:optimove-android:7.+'
    // Add library dependency to retrieve FCM push tokens
    implementation platform('com.google.firebase:firebase-bom:28.2.0')
    implementation 'com.google.firebase:firebase-messaging'
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

Download the google-services.json file from your Firebase app 'General' settings, and add it to your app/ folder.

Now you can add the Optimove FirebaseMessagingService and PushBroadcastReceiver to your AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example">

    <!-- Optionally add the wake lock permission to stop the CPU from sleeping when a message is received -->
    <!-- <uses-permission android:name="android.permission.WAKE_LOCK" /> -->
    <!-- Optionally add the boot completed permission to allow periodic tasks to persist across phone reboots -->
    <!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -->

    <application
        android:name=".ExampleApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        ...

       <!-- Optimove FCM handler -->
        <service android:name="com.optimove.android.optimobile.FirebaseMessagingService" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <!-- Optimove Push receiver -->
        <receiver android:name="com.optimove.android.optimobile.PushBroadcastReceiver" android:exported="false">
            <intent-filter>
                <action android:name="com.optimove.push.RECEIVED" />
                <action android:name="com.optimove.push.OPENED" />
                <action android:name="com.optimove.push.DISMISSED" />
                <action android:name="com.optimove.push.BUTTON_CLICKED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Whenever appropriate, opt-in the device for push notifications (Starting from Android 13, this method will open the notification permission request prompt):

Optimove.initialize(this, new OptimoveConfig.Builder(
    "<YOUR_OPTIMOVE_CREDENTIALS>","<YOUR_OPTIMOVE_MOBILE_CREDENTIALS>"
).build());

Optimove.getInstance().pushRequestDeviceToken();

N.B. make sure to call after the SDK has been initialized

⚠️ **GitHub.com Fallback** ⚠️