Push messaging for Android

1. Using FCM Unity Plugin

  1. Import FCM Unity plugin as instructed here into your Unity project.

  2. If you have replaced the Assets/Plugins/Android/AndroidManifest.xml then make sure to add back your WebEngage license-code and debug-mode meta-data tags in the AndroidManifest.xml file.

  3. In your script where you have registered callbacks for OnTokenReceived and OnMessageReceived, add the following code snippets.

using WebEngageBridge;
    ...
    Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
        var dependencyStatus = task.Result;
        if (dependencyStatus == Firebase.DependencyStatus.Available)
        {
            Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
            Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
        }
        else
        {
            ...
        }
    });
    
    public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    {
        ...
        WebEngage.SetPushToken(token.Token);
    }

    public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
    {
        Dictionary<string, string> data = new Dictionary<string, string>(e.Message.Data);
        if (data.ContainsKey("source") && "webengage".Equals(data["source"]))
        {
            WebEngage.SendPushData(data);
        }
        ...
    }

📘

Note: Push notifications will work as expected when app is in foreground. The drawback of this approach is that push notifications will not be shown when app is in background. However those push notifications are cached and will be shown on next app launch. If you wish to prevent this drawback, then follow the Overriding FCM Unity Plugin approach given below.

2. Overriding FCM Unity Plugin

  1. Import FCM Unity plugin as instructed here into your Unity project.

  2. If you have replaced the Assets/Plugins/Android/AndroidManifest.xml then make sure to add back your WebEngage license-code and debug-mode meta-data tags in the AndroidManifest.xml file.

  3. Download and add the webengage-android-fcm.aar file in Assets/Plugins/Android/ directory of your Unity project.

  4. Add the following service tag in your Assets/Plugins/Android/AndroidManifest.xml file as shown below.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    ...>

    <application
        ...>

        <service android:name="com.webengage.android.fcm.WebEngageFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        ...
    </application>
</manifest>
  1. Update the FCM registration token on app start as shown below.
using WebEngageBridge;
    ...

    WebEngage.UpdateFcmToken();

So, what's next?