Push Messaging
Before continuing, please ensure that you've added the Flutter SDK to your app.
For Android
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 enable Push Messaging for your Android app via the WebEngage Flutter SDK.
Step 1: Add Firebase to Your Project by following the necessary steps in FCM Docs
Step 2: Add the below dependencies in the app-level build.gradle
file.
implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-messaging:20.2.1'
implementation 'com.google.android.gms:play-services-ads:15.0.1'
Step 3: Add the following to your dependencies section in project/build.gradle
file.
classpath 'com.google.gms:google-services:4.3.4'
Step 4: Pass Firebase tokens 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);
}
}
import com.google.firebase.messaging.FirebaseMessagingService;
import com.webengage.sdk.android.WebEngage;
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(s: String) {
super.onNewToken(s)
WebEngage.get().setRegistrationID(s)
}
}
Highly Recommended! Passing Firebase tokens to WebEngage from the onCreate
method of your Application class ensures that changes in the user’s Firebase token are communicated to WebEngage. Here's how you can execute this.
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.android.gms.tasks.OnCompleteListener;
import androidx.annotation.NonNull;
import com.webengage.sdk.android.WebEngage;
public class MainApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
WebEngage.get().setRegistrationID(token);
}
});
}
}
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.android.gms.tasks.OnCompleteListener;
import androidx.annotation.NonNull;
import com.webengage.sdk.android.WebEngage;
class MainApplication : FlutterApplication() {
fun onCreate() {
super.onCreate()
FirebaseMessaging.getInstance().token
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
[email protected]
}
// Get new FCM registration token
val token: String = task.getResult()
WebEngage.get().setRegistrationID(token)
})
}
}
Step 5: Pass messages to WebEngage.
Create a class that extends FirebaseMessagingService
and pass messages to WebEngage.
As shown below, all incoming messages from WebEngage will contain a key source with webengage
as the corresponding value.
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);
}
}
}
}
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 fun onMessageReceived(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
if (data != null) {
if (data.containsKey("source") && "webengage" == data["source"]) {
WebEngage.get().receive(data)
}
}
}
}
Step 6: Register the service to the application element of your AndroidManifest.xml
file as shown below.
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Step 7. Add your Firebase Credentials to WebEngage
And you're good to go!
For iOS
Follow our native iOS Push Messaging Guide to configure Push Messaging for your iOS Flutter app.
Please feel free to drop in a few lines at [email protected] in case you have any queries or feedback. We're always just an email away!
Updated 10 days ago