Skip to main content

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()

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
)

Step 2 - Handle Notification in FirebaseMessagingService

  1. Create a class which extends FirebaseMessagingService (if not already present) which will receive the triggered notification.
class MessagingService: FirebaseMessagingService() {
override fun onCreate() {
super.onCreate()
}

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
}
}
  1. Create a instance of LMChatNotificationHandler in the onCreate() method of the Service and pass instance of application and Intent of the launcher activity.
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)
}
}
  1. Add handleNotification() method in onMessageReceived() method and pass the data received in the same function.
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)
}
}