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
-
Configure
google-services.jsonfile: Download thegoogle-services.jsonfile 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.jsonfile in theAssets/Firebasefolder within your Unity project. - Method 2 (Alternative): Converting to XML:
You can convert the content of the
google-services.jsonfile to XML format and place it in your Android project’sstrings.xmlfile. For more details on this method and the required fields, refer to the Google Services Plugin documentation .
- Enter Push Credentials in the Metrix Dashboard:
- Log in to your Metrix dashboard.
- Go to Application Settings.
- Select the
Channel Integrationtab, then click onApp Push. - On this page, enter your Firebase project settings into the relevant fields.
- Save the values by clicking the
savebutton. - Note: To obtain the
Service account Datafile (which includes your project’sprivate key), you can go to your Firebase console’sProject settings, select theService Accountstab, and click theGenerate new private keybutton.
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
FirebaseMessagingServicein your Android project and don’t want to add the Metrix service separately inAndroidManifest.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’sonMessageReceivedmethod, callMetrixNotification.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 returnstrue, 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
MetrixNotificationis 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 yourAndroidManifest.xmlfile:<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>⚠️WarningChoose 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:
-
Disable Auto-loading in
AndroidManifest.xml(Android Only): Set themetrix_auto_load_in_app_messagesvalue in your application’sAndroidManifest.xmlfile:<manifest> ... <application> ... <meta-data android:name="metrix_auto_load_in_app_messages" android:value="false" /> </application> </manifest> -
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.