> ## Documentation Index
> Fetch the complete documentation index at: https://docs.castled.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Firebase Push Notification Setup

> [!NOTE]
>
> **Prerequisites**
>
> * [Firebase SDK integrated with your app](https://firebase.google.com/docs/ios/setup)
> * Firebase private key uploaded in Castled dashboard. More details can be found [here](https://docs.castled.io/developer-resources/sdk-integration/android/fcm-private-key-setup.md)

If your iOS app is using Firebase for sending push notifications you can follow the steps below when integrating Castled SDK.

1. Complete the basic [Push Notifications](https://docs.castled.io/developer-resources/sdk-integration/ios/push-notifications.md) setup.
2. If you are using rich push notification, checkout the additional [steps](https://docs.castled.io/developer-resources/sdk-integration/ios/rich-push-notification.md).
3. [Disable swizzling](https://docs.castled.io/developer-resources/sdk-integration/ios/swizzling.md).
4. Notify Castled SDK about push tokens. Please find the code snippet for this below.
   * Monitoring token refresh

     ```swift swift
     extension AppDelegate: MessagingDelegate {
         // [START refresh_token]
         func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
             print("Firebase registration token: \(String(describing: fcmToken))")
             let dataDict: [String: String] = ["token": fcmToken ?? ""]
             NotificationCenter.default.post(
                 name: Notification.Name("FCMToken"),
                 object: nil,
                 userInfo: dataDict
             )
             if let token = fcmToken {
                Castled.sharedInstance.setPushToken(token, type: .fcm)
             }
         }
         // [END refresh_token]
     }
     ```

     ```objc objc
     - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
         NSLog(@"FCM registration token: %@", fcmToken);
         // Notify about received token.
         NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
         [[NSNotificationCenter defaultCenter] postNotificationName:
         @"FCMToken" object:nil userInfo:dataDict];
         [[Castled sharedInstance] setPushToken:fcmToken type:CastledPushTokenTypeFcm];
         // TODO: If necessary send token to application server.
         // Note: This callback is fired at each app startup and whenever a new token is generated.
     }

     ```

   * If fetching token manually, you can notify the SDK about the the new token as below.

     ```swift swift
     Messaging.messaging().token { token, error in
         if let error = error {
             print("Error fetching FCM registration token: \(error)")
         } else if let token = token {
             print("FCM registration token: \(token)")
             Castled.sharedInstance.setPushToken(token, type: .fcm)
         }
     }

     ```

     ```objc objc
      [[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) {
         if (error != nil) {
             NSLog(@"Error getting FCM registration token: %@", error);
         } else {
             NSLog(@"FCM registration token: %@", token);
             [[Castled sharedInstance] setPushToken:token type:CastledPushTokenTypeFcm];
         }
         }];


     ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.