Push Notifications
Push notifications allow you to engage users and deliver timely updates and alerts, even when your app is not running. This guide outlines the steps to integrate push notification functionality for LikeMinds Chat SDK in your app.
Refer to Notification List to see the list of notifications triggered.
Prerequisites
Before proceeding with the integration, make sure you have the following prerequisites in place:
- FCM Service Account Keys: Obtain the Firebase Cloud Messaging service account keys from the Firebase Console. Here is the guide to obtain the same.
Implementation
Step 1 - Enable Push Notification and Share Device Id
While setting up LikeMinds Chat SDK in onCreate()
method of the Application class, send enablePushNotifications
as true
and user's deviceId
in LMChatCore.setup()
- Kotlin
- Java
val application = this
val enablePushNotifications = false
val deviceId = "ENTER USER'S DEVICE ID"
val domain = "ENTER YOUR DOMAIN HERE"
val lmChatCoreCallback = object : LMChatCoreCallback {}
LMChatCore.setup(
application = application,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId,
domain = domain,
lmChatCoreCallback = lmChatCoreCallback
)
Application application = this; // instance of the application
LMChatTheme chatTheme = null; // instance of the theme
String domain = "ENTER YOUR DOMAIN"; // domain of the app
boolean enablePushNotifications = true; // enable or disable push notifications
String deviceId = "ENTER USER'S DEVICE ID"; // device id of the user
LMChatCoreCallback lmChatCoreCallback = new LMChatCoreCallback() {};
LMChatCore.INSTANCE.setup(
application,
lmChatCoreCallback,
chatTheme,
domain,
enablePushNotifications,
deviceId
);
Step 2 - Handle Notification in FirebaseMessagingService
- Create a class which extends
FirebaseMessagingService
(if not already present) which will receive the triggered notification.
- Kotlin
- Java
class MessagingService: FirebaseMessagingService() {
override fun onCreate() {
super.onCreate()
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
}
}
public class MessagingService extends FirebaseMessagingService {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
}
}
- Create a instance of
LMChatNotificationHandler
in theonCreate()
method of theService
and pass instance ofapplication
andIntent
of the launcher activity.
- Kotlin
- Java
class MessagingService: FirebaseMessagingService() {
private lateinit var mNotificationHandler: LMChatNotificationHandler
override fun onCreate() {
super.onCreate()
mNotificationHandler = LMChatNotificationHandler.getInstance()
val launcherIntent = Intent(this, <class of launcher activity>).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
mNotificationHandler.create(this.application, launcherIntent)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
}
}
public class MessagingService extends FirebaseMessagingService {
LMChatNotificationHandler mNotificationHandler;
@Override
public void onCreate() {
super.onCreate();
mNotificationHandler = LMChatNotificationHandler.getInstance();
Intent launcherIntent = new Intent(this, AfterLoginActivity.class);
launcherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
mNotificationHandler.create(this.getApplication(), launcherIntent);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
}
}
- Add
handleNotification()
method inonMessageReceived()
method and pass the data received in the same function.
- Kotlin
- Java
class MessagingService: FirebaseMessagingService() {
private lateinit var mNotificationHandler: LMChatNotificationHandler
override fun onCreate() {
super.onCreate()
mNotificationHandler = LMChatNotificationHandler.getInstance()
val launcherIntent = Intent(this, <class of launcher activity>).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
mNotificationHandler.create(this.application, launcherIntent)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
mNotificationHandler.handleNotification(message.data)
}
}
public class MessagingService extends FirebaseMessagingService {
LMChatNotificationHandler mNotificationHandler;
@Override
public void onCreate() {
super.onCreate();
mNotificationHandler = LMChatNotificationHandler.getInstance();
Intent launcherIntent = new Intent(this, AfterLoginActivity.class);
launcherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
mNotificationHandler.create(this.getApplication(), launcherIntent);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
mNotificationHandler.handleNotification(message.getData());
}
}