Skip to main content

Push Notifications

Push notifications are essential for engaging users and keeping them informed about important updates and events within your iOS application. This guide will walk you through the process of setting up push notifications for use with the LikeMinds iOS Chat SDK.

Refer to Notification List to see the list of notifications triggered.

Prerequisites

Before proceeding, ensure that:

  1. You have set up Firebase for your iOS project.
  2. You have enabled Push Notifications and Background Modes (Remote Notifications) in Xcode.
  3. You have an APNs certificate or APNs authentication key registered in Firebase.
  4. GCP Service Account File: Obtain the GCP service account file from the Firebase Console. Here is the guide to use them.

Steps to Integrate Notifications

1. Configure Firebase in Your App

  1. Add Firebase to your iOS app by following Firebase Setup Guide.
  2. Download the GoogleService-Info.plist file from Firebase Console and add it to your Xcode project.

2. Register for Push Notifications

Modify your AppDelegate.swift to configure Firebase and register for notifications.

Add Required Imports

import FirebaseCore
import FirebaseMessaging
import LikeMindsChatCore
import LikeMindsChatUI
import UIKit
import UserNotifications

Configure Firebase and Register for Notifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
registerForPushNotifications(application: application)

let deviceId = UIDevice.current.identifierForVendor?.uuidString
LMChatCore.shared.setupChat(deviceId: deviceId)

return true
}
}

3. Handle Notification Permissions

To request notification permissions, implement the registerForPushNotifications(application:) method.

private func registerForPushNotifications(application: UIApplication) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (granted, error) in
guard granted else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}

4. Handle APNs Token Registration

When the device registers successfully for remote notifications, send the APNs token to Firebase.

func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Messaging.messaging().apnsToken = deviceToken
}

If registration fails, print the error:

func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error
) {
print("Failed to register for notifications: \(error.localizedDescription)")
}

5. Implement UNUserNotificationCenterDelegate

Extend UNUserNotificationCenterDelegate to handle push notifications in different app states.

extension AppDelegate: UNUserNotificationCenterDelegate {

}

Foreground Notifications

To handle notifications while the app is in the foreground:

func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
LMChatCore.shared.willPresentNotification(
userInfo: notification.request.content.userInfo,
withCompletionHandler: completionHandler
)
}

Background & Tapped Notifications

When the user taps a notification, LikeMinds Chat SDK will process it:

func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
LMChatCore.shared.didReceieveNotification(
userInfo: response.notification.request.content.userInfo
)
}

6. Handle Firebase Token Updates

Firebase assigns a registration token to each device. Capture this token when it's updated.

extension AppDelegate: MessagingDelegate {
func messaging(
_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?
) {
print("Firebase registration token: \(String(describing: fcmToken))")
}
}

7. Enable Background Modes for Push Notifications

To ensure your app receives push notifications in the background:

  1. Go to Xcode → Project Settings → Signing & Capabilities.
  2. Click + Capability and add Background Modes.
  3. Enable Remote Notifications.

Summary

You have now successfully integrated push notifications in your LikeMinds Chat iOS app using Firebase Cloud Messaging and APNs. This allows users to receive real-time notifications about chat activity.

For more advanced use cases, refer to:

🚀 Your chat app is now ready to receive push notifications!