Skip to Content
AI Powered Docs! 🤖 These docs are translated with AI, so keep an eye out for minor quirks. We're always improving!

Messaging

Metrix allows you to send targeted messages to your users in your Unity application through various communication channels. This section guides you through implementing two primary messaging channels: Push Notification and In-App Message.


1. Push Notification Implementation (Android Only)

To send push notifications via Metrix, you need to use Firebase Cloud Messaging (FCM) in your Android project and configure the necessary settings in your Metrix dashboard and your application’s code.

1.1. Firebase Configuration in Your Project and Metrix Dashboard

  1. Configure google-services.json file: Download the google-services.json file from your Firebase project console. Please note that this file is not automatically recognized by Unity/Android and must be properly configured in your project for the Firebase SDK to identify it.

    There are two main methods to do this:

  • Method 1 (Recommended): Using Firebase Unity SDK: The best and most reliable way to ensure correct configuration is by using the official Firebase Unity SDK plugin. After importing this plugin into your Unity project, you should place the google-services.json file in the Assets/Firebase folder within your Unity project.
  • Method 2 (Alternative): Converting to XML: You can convert the content of the google-services.json file to XML format and place it in your Android project’s strings.xml file. For more details on this method and the required fields, refer to the Google Services Plugin documentation.
  1. Enter Push Credentials in the Metrix Dashboard:
  • Log in to your Metrix dashboard.
  • Go to Application Settings.
  • Select the Channel Integration tab, then click on App Push.
  • On this page, enter your Firebase project settings into the relevant fields.
  • Save the values by clicking the save button.
  • Note: To obtain the Service account Data file (which includes your project’s private key), you can go to your Firebase console’s Project settings, select the Service Accounts tab, and click the Generate new private key button.

1.2. Enabling Push Notification Capability in Unity

Add Service to AndroidManifest.xml: To receive and manage FCM messages via Metrix, you can implement one of the two following methods in your application’s AndroidManifest.xml file:

  • Method 1 (Recommended): Using MetrixFirebaseMessagingService: This is the default Metrix service optimized for receiving FCM messages. Place the following within the <application> tag:

    <manifest> ... <application> ... <service android:name="ir.metrix.notification.receivers.MetrixFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> </application> </manifest>

    Note for Projects with Existing FirebaseMessagingService:

    If you already have a custom FirebaseMessagingService in your Android project and don’t want to add the Metrix service separately in AndroidManifest.xml, you can manage Metrix messages through your existing service by adding one line of code. This allows you to maintain your own FCM message receiving logic while still processing Metrix messages.

    In your FirebaseMessagingService’s onMessageReceived method, call MetrixNotification.onMessageReceived(message). It’s important that this line of code comes before your own FCM message processing logic. If Metrix processes the message, the method returns true, and you can skip further processing for that message.

    import ir.metrix.notification.MetrixNotification; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; public class CustomFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage message) { // Pass the message to Metrix. If Metrix processes it, it returns true. if (MetrixNotification.onMessageReceived(message)) { return; // Metrix handled this message, no further processing needed for Metrix messages } // If Metrix did not handle the message, process your own FCM messages here // your own fcm messages processing logic } }

    Note: Make sure MetrixNotification is accessible in your project.

  • Method 2: Using FCMBroadcastReceiver (Alternative): This method uses a Broadcast Receiver to receive FCM messages. Note: If you use this method, you must create a unique permission for your app and use it so that only your app can receive your Metrix Notification messages. Add the following to your AndroidManifest.xml file:

    <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
    <receiver android:name="ir.metrix.notification.receivers.FCMBroadcastReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter android:priority="999"> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="${applicationId}" /> </intent-filter> </receiver>
    ⚠️
    Warning

    Choose and implement only ONE of Method 1 or Method 2 for adding the push notification service/receiver in your AndroidManifest.xml.


2. In-App Message Implementation

In-App messages are displayed directly within your application to the user and are very useful for real-time contextual interactions.

2.1. Controlling Automatic Message Loading

By default, the Metrix SDK loads and displays in-app messages every time the application is opened (or comes to the foreground). If you want more control over when and where messages are displayed and prefer to show them on a specific screen, you can disable automatic loading:

  1. Disable Auto-loading in AndroidManifest.xml (Android Only): Set the metrix_auto_load_in_app_messages value in your application’s AndroidManifest.xml file:

    <manifest> ... <application> ... <meta-data android:name="metrix_auto_load_in_app_messages" android:value="false" /> </application> </manifest>
  2. Manually Load Messages in C# Code: After disabling auto-loading, you can call the Metrix.LoadInAppMessages() method anywhere in your C# code to load and display in-app messages:

    // Call this method in your C# script when you want to load and display in-app messages Metrix.LoadInAppMessages();

Note: In-app message auto-loading settings are controlled via AndroidManifest.xml for the Android platform.