Don Peter

Nov 24, 20233 min

Push Notifications in iOS Swift Apps with Firebase Cloud Messaging (FCM)

Push notifications play a crucial role in keeping users engaged with your iOS Swift app by delivering timely updates and personalized content. Firebase Cloud Messaging (FCM) is a robust and reliable solution for implementing push notifications in your iOS apps.

In this guide, we'll explore why you should use FCM, how to integrate it into your Swift app, and when it's most beneficial.

Why Firebase Cloud Messaging (FCM) ?

Cross-Platform Compatibility

FCM offers seamless integration across various platforms, including iOS and Android. This ensures a consistent messaging experience for users using different devices.

Reliability and Scalability

Firebase Cloud Messaging is built on Google Cloud Platform, providing a reliable and scalable infrastructure for handling push notifications, regardless of the size of your user base.

Cloud Functions Integration

FCM integrates seamlessly with Firebase Cloud Functions, allowing you to perform serverless operations triggered by push notifications. This enables you to update user data or perform other backend tasks effortlessly.

How to Integrate FCM in Your iOS Swift App

Set Up a Firebase Project

  • Create a new Firebase project on the Firebase Console.

  • Add your iOS app to the project, and download the GoogleService-Info.plist file.

Install the Firebase SDK

Open your Xcode project and install the Firebase SDK using CocoaPods. Add the following to your Podfile:


 
pod 'Firebase/Core'
 
pod 'Firebase/Messaging'
 

Run pod install in the terminal.

Configure Your App Delegate

In your AppDelegate.swift, import Firebase and configure it in the didFinishLaunchingWithOptions method:


 
import Firebase
 

 
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
FirebaseApp.configure()
 
// Other setup code
 
return true
 
}
 

Register for Remote Notifications

  • Request user permission to receive notifications and register for remote notifications in the didFinishLaunchingWithOptions method:


 
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
// ... (previous setup code)
 
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
 
if granted {
 
DispatchQueue.main.async {
 
application.registerForRemoteNotifications()
 
}
 
}
 
}
 
// ...
 
}
 

Implement FCM Delegate Methods

Add the following methods to receive FCM tokens and handle incoming messages:


 
import FirebaseMessaging
 

 
extension AppDelegate: MessagingDelegate {
 
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
 
print("FCM Token: \(fcmToken)")
 
}
 

 
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
 
print("Received data message: \(remoteMessage.appData)")
 
}
 
}
 

Handle FCM Token and Notifications

Retrieve the FCM token and handle notifications:


 
import FirebaseMessaging
 

 
class YourViewController: UIViewController {
 
override func viewDidLoad() {
 
super.viewDidLoad()
 

 
if let token = Messaging.messaging().fcmToken {
 
print("FCM Token: \(token)")
 
}
 
}
 
}
 

The FCM token is a unique identifier assigned to a specific instance of an app on a device. FCM tokens are not permanent and can change. For example, if a user reinstalls your app or clears app data, a new FCM token will be generated.

It's essential to handle token refresh in your app and update your server with the new token when it changes. This ensures that your server can continue to send messages to the correct device.

When to Use FCM Push Notifications

Real-Time Updates

Utilize FCM for delivering real-time updates to users, such as new messages, friend requests, or other time-sensitive information.

Personalized Content

Send personalized notifications based on user behavior or preferences to enhance the user experience and encourage engagement.

Re-Engagement Campaigns

Use push notifications to re-engage users who haven't interacted with your app for a while. Deliver relevant content to bring them back.

Implementing FCM push notifications in your iOS Swift app is a powerful way to enhance user engagement and deliver timely information. With its cross-platform compatibility, reliability, and ease of integration, Firebase Cloud Messaging is a top choice for developers seeking an efficient push notification solution.