Push Messaging

🚧

Important

Please ensure that you have completed all the platform integration steps listed under Getting started before proceeding

Push Notifications are messages that pop up on mobile devices. App publishers can send them at any time; even if the recipients aren’t currently engaging with the app or using their devices.
You can start sending Push Notifications to your Android users via WebEngage by configuring Firebase Cloud Messaging (FCM).

Here's how you can go about it:

1. Add Firebase to Your Project

Please follow these instructions for adding Firebase to Your Android Project.

2. Add Firebase Cloud Messaging Dependency

Add the dependency for Firebase Cloud Messaging in your module’s build.gradle.

dependencies {
  implementation 'com.google.firebase:firebase-messaging:17.3.2'
}

3. Pass Firebase Token to WebEngage

Firebase tokens can be passed to WebEngage using FirebaseMessagingService

import com.google.firebase.messaging.FirebaseMessagingService;
import com.webengage.sdk.android.WebEngage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        WebEngage.get().setRegistrationID(s);
    }
}

It is also recommended that you pass Firebase token to WebEngage from onCreate of your Application class as shown below. This will ensure that changes in user’s Firebase token are communicated to WebEngage.

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.webengage.sdk.android.WebEngage;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
    
        FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
            @Override
            public void onSuccess(InstanceIdResult instanceIdResult) {
                String token = instanceIdResult.getToken();
                WebEngage.get().setRegistrationID(token);
            }
        });
    }
}

4. Pass Messages to WebEngage

Create a class that extends FirebaseMessagingService and pass messages to WebEngage.
All incoming messages from WebEngage will contain key source with the value as webengage.

package your.application.package;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.webengage.sdk.android.WebEngage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    if(data != null) {
      if(data.containsKey("source") && "webengage".equals(data.get("source"))) {
        WebEngage.get().receive(data);
      }
    }
  }
}

Next, register the service to the application element of your AndroidManifest.xml as follows.

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

5. Add Your Firebase Credentials to WebEngage

Step 1: Log in to the Firebase Developers Console.

Step 2: Select your Firebase project.

Step 3: Navigate to Project Settings > Cloud Messaging and copy your server key as shown in the image below.

Step 4: Log in to your WebEngage dashboard and navigate to Data Platform > Integrations > Channels.

  • Select Push and paste the copied server key under the field labeled “GCM/FCM Server Key” under the Android tab.

  • Enter your application package name under the field labeled “Package Name” and click Save.

Additional Step: Customizing Icon & Accent Colors

(Applicable only to Android SDK v1.14.0 & above)

By default, the WebEngage Android SDK uses your application's icon to set the small and large icons of your push messages. You can provide custom icons during SDK initialization, as shown below.

import com.webengage.sdk.android.WebEngageConfig;
import com.webengage.sdk.android.WebEngageActivityLifeCycleCallbacks;

...
...

WebEngageConfig webEngageConfig = new WebEngageConfig.Builder()
    .setPushSmallIcon(R.drawable.YOUR_SMALL_ICON)
    .setPushLargeIcon(R.drawable.YOUR_LARGE_ICON)
    .setPushAccentColor(Color.parseColor("#ff0000"))
    .build();
registerActivityLifecycleCallbacks(new WebEngageActivityLifeCycleCallbacks(this, webEngageConfig));

Notification Icon Fix for Android Lollipop & Above

Since the release of Lollipop (API 21), the material design style dictates that all non-alpha channels in notification icons be ignored. Therefore, the small icon will be rendered as solid white if it's not transparent. Hence, if you have provided a custom icon during SDK initialization, then you need to make sure that it is transparent.

Please feel free to drop in a few lines at [email protected] in case you have any further queries. We're always just an email away!

Updated 16 days ago


Push Messaging


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.