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 LikeMindsFeed SDK in your app.

Prerequisites

Before proceeding with the integration, make sure you have the following prerequisites in place:

  1. Firebase Server Key: Obtain the Firebase Server Key from the Firebase Console. This key is required for sending push notifications through Firebase Cloud Messaging (FCM).
  2. LikeMinds API Key: Obtain your LikeMinds API Key from the LikeMinds dashboard. This key is required to initialize the LikeMinds SDK in your app.

Step 1: Set up the Firebase Server Key

In the LikeMinds dashboard, navigate to the "Push Notifications" section and update the Firebase Server Key with the key obtained from the Firebase Console. You can find a detailed tutorial here.

Step 2: Register the Device for Push Notifications

To receive push notifications, you need to register the device with LikeMinds by providing the FCM token. Follow these steps:

  1. Obtain the FCM token from the Firebase SDK.
  2. Call the registerDeviceToken(with:deviceID:completion:) function provided by the LikeMinds SDK with the following parameters:
    • fcmToken: The FCM token obtained from the Firebase SDK.
    • deviceID: A unique identifier for the device (e.g., UUID or a user-specific identifier).
    • completion: An optional completion block that will be called with the result of the registration process. It provides a Result enum value indicating whether the registration was successful or not.

Here's an example implementation:

import Firebase
import LikeMindsFeed

func registerForPushNotifications() {
Messaging.messaging().token { fcmToken, error in
if let error = error {
print("Error fetching FCM token: \(error)")
return
}

guard let fcmToken = fcmToken else {
print("Failed to fetch FCM token")
return
}

let deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""
LMFeedCore.shared.registerDeviceToken(with: fcmToken, deviceID: deviceID) { result in
switch result {
case .success:
print("Device registered for push notifications")
case .failure(let error):
print("Failed to register device: \(error.localizedDescription)")
}
}
}
}

That's it! You have now successfully integrated push notification functionality into your iOS app using the LikeMinds SDK. Now you can send all the feed related notifications mentioned here

Handle Notification

When a notification is received, it should be forwarded to the LMFeedCore.shared.didReceiveNotification method. This method asynchronously processes the notification and returns a result indicating whether a view controller should be presented or if an error occurred.

Here's an example implementation:

func handleNotification(notification: UNNotificationRequest) {
// Pass the received notification to the LMFeedCore for processing.
LMFeedCore.shared.didReceiveNotification(notification) { result in
// Handle the result of the notification processing.
switch result {
case .success(let viewController):
// Present viewController accordingly
case .failure(let error):
// Handle error accordingly
}
}
}