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 Feed 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 Feed SDK in onCreate()
method of the Application class, send enablePushNotifications
as true
and user's deviceId
in LMFeedCore.setup()
- Kotlin
- Java
val application = this
val enablePushNotifications = false
val deviceId = "ENTER USER'S DEVICE ID"
val domain = "ENTER YOUR DOMAIN HERE"
val lmFeedCoreCallback = object : LMFeedCoreCallback {}
LMFeedCore.setup(
application = application,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId,
domain = domain,
lmFeedCoreCallback = lmFeedCoreCallback
)
Application application = this; // instance of the application
LMFeedSetThemeRequest feedTheme = 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
LMFeedCoreCallback lmFeedCoreCallback = new LMFeedCoreCallback() {};
LMFeedCore.INSTANCE.setup(
application,
lmFeedCoreCallback,
feedTheme,
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
LMFeedNotificationHandler
in theonCreate()
method of the Service
- Kotlin
- Java
class MessagingService: FirebaseMessagingService() {
private lateinit var mNotificationHandler: LMFeedNotificationHandler
override fun onCreate() {
super.onCreate()
mNotificationHandler = LMFeedNotificationHandler.getInstance()
mNotificationHandler.create(this.application)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
}
}
public class MessagingService extends FirebaseMessagingService {
LMFeedNotificationHandler mNotificationHandler;
@Override
public void onCreate() {
super.onCreate();
mNotificationHandler = LMFeedNotificationHandler.getInstance();
mNotificationHandler.create(this.getApplication());
}
@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: LMFeedNotificationHandler
override fun onCreate() {
super.onCreate()
mNotificationHandler = LMFeedNotificationHandler.getInstance()
mNotificationHandler.create(this.application)
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
mNotificationHandler.handleNotification(message.data)
}
}
public class MessagingService extends FirebaseMessagingService {
LMFeedNotificationHandler mNotificationHandler;
@Override
public void onCreate() {
super.onCreate();
mNotificationHandler = LMChatNotificationHandler.getInstance();
mNotificationHandler.create(this.getApplication());
}
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
mNotificationHandler.handleNotification(message.getData());
}
}