# Integrate offline push (/en/realtime-media/im/build/notifications-and-event-handling/offline-push/integrate-test/flutter)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

Follow this guide to integrate and test push notifications in your environment.

    ## Prerequisites [#prerequisites-2]

    Before proceeding, ensure that you meet the following requirements:

    * You have initialized the Chat SDK. For details, see [SDK quickstart](../../../get-started-sdk).
    * You understand the call frequency limit of the Chat APIs supported by different pricing plans as described in [Limitations](../../limitations).

    ## Integrate FCM push [#integrate-fcm-push-1]

    This section guides you through how to integrate FCM with Chat.

    ### 1. Create a project in Firebase [#1-create-a-project-in-firebase-1]

    1. Log in to the [Firebase console](https://console.firebase.google.com/) and click **Add project**.

    2. In the **Create a project** page, enter a project name, and click **Create project**.

       Note: You can toggle off Enable Google Analytics for this project if this option is not needed.

    3. After the project is ready, click **Continue** to redirect to the project page. Then click the **Flutter** icon and follow the **Firebase CLI** to set your project.

    4. In the **Project settings** page, perform the following operations:

       * For Android apps, find the **Android apps** in **Your apps**, and set your Android app according to the [SDK instructions](#1-create-a-project-in-firebase).

       * For iOS apps, find the **Apple apps** in **Your apps**, and set your Apple app according to the [SDK instructions](#1-create-push-certificates).

    5. In the project page, click the project you have created.

    6. Query the sender ID. On the **Project settings** page, select the **Cloud Messaging** tab and view **Sender ID**. When uploading the FCM certificate to Agora Console, set the certificate name to the FCM sender ID.

       ![push\_fcm\_senderid.png](https://assets-docs.agora.io/images/im/push_fcm_senderid.png)

    7. On the **Project settings** page, select the **Service accounts** tab and click **Generate new private key** to generate a JSON file. Save this file and upload it to Agora Console when using the V1 certificate.

       ![push\_fcm\_private\_key](https://assets-docs.agora.io/images/im/push_fcm_private_key.png)

    ### 2. Upload FCM certificate to Agora Console [#2-upload-fcm-certificate-to-agora-console-1]

    After successfully logging into Chat, you can upload the FCM push certificate to Agora Console:

    1. Log in to [Agora Console](https://console.agora.io/v2), and click **Project Management** in the left navigation bar.

    2. On the **Project Management** page, locate the project that has Chat enabled and click **Config**.

    3. On the project edit page, click **Config** next to **Chat**.

    4. On the project config page, select **Features** > **Push Certificate** and click **Add Push Certificate**.

    5. In the pop-up window, select the **Google** tab, and configure the following fields:

       ![push\_fcm\_add\_certificate](https://assets-docs.agora.io/images/im/push_fcm_add_certificate.png)

       | Parameter            | Type   | Required | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
       | :------------------- | :----- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
       | **Certificate Type** |        | No       | Select whether to use a V1 certificate or a legacy certificate. **V1**: Recommended. You need to configure a **Private Key**. **Legacy**: Will soon be deprecated. You need to configure a **Push Key**.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
       | **Private Key**      | file   | Yes      | Click **Generate new private key** on the **Project settings** > **Service accounts** page of the [Firebase Console](https://console.firebase.google.com) to generate the `.json` file, then upload it to Agora Console.                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
       | **Push Key**         | String | Yes      | FCM Server Key. Obtain the server key in the &#x2A;*Cloud Messaging API (Legacy)** area of the **Project settings** > **Cloud Messaging** page of the [Firebase Console](https://console.firebase.google.com).   &#x2A;*This parameter is only valid for legacy certificates.**                                                                                                                                                                                                                                                                                                                                                                                                         |
       | **Certificate Name** | String | Yes      | The sender ID configured for the FCM. For the new version of the certificate, you can find the sender ID on the **Project settings** > **Cloud Messaging** page of the [Firebase Console](https://console.firebase.google.com). For legacy certificates, go to the \***Project settings > Cloud Messaging** page of the [Firebase Console](https://console.firebase.google.com), and get the sender ID in the &#x2A;*Cloud  Messaging API (Legacy)** area.  The certificate name is the only condition used by the Agora server to determine which push channel the target device uses, so ensure that the sender set when integrating FCM in Chat is consistent with what is set here. |
       | **Sound**            | String | No       | The ringtone flag for when the receiver gets the push notification.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
       | **Push Priority**    |        | No       | Message delivery priority. See [Setting the priority of a message](https://firebase.google.cn/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
       | **Push Msg Type**    |        | No       | The type of the message sent to the client through FCM. See [Message types](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages): **Data**: Data message, processed by the client application. **Notification**: Notification message, automatically processed by FCM SDK. **Both**: Notification messages and data messages can be sent through the FCM client.                                                                                                                                                                                                                                                                           |

    #### Switch from legacy to the V1 certificate [#switch-from-legacy-to-the-v1-certificate-1]

    The legacy HTTP or XMPP API is being retired on June 20, 2024. In view of this, switch to the latest FCM API (HTTP v1) version of the certificate as soon as possible. See [Firebase Console](https://console.firebase.google.com) for details.

    Make sure that the uploaded V1 certificate is available, as the legacy one will be deleted upon upload. If the new certificate is not available, the push will fail.

    Take the following steps to switch from the old to the new certificate:

    1. Click **Edit** in the **Action** column of the old certificate on the **Push Certificate** page.

       ![push\_fcm\_oldcertificate\_edit](https://assets-docs.agora.io/images/im/push_fcm_oldcertificate_edit.png)

    2. In the **Google** tab of the **Edit Push Certificate** window, switch the **Certificate Type** to **V1**.

       ![push\_fcm\_oldcertificate\_switch](https://assets-docs.agora.io/images/im/push_fcm_oldcertificate_switch.png)

    3. Click **Upload** to upload the locally saved V1 certificate file (.json).

       ![push\_fcm\_newcertificate\_upload](https://assets-docs.agora.io/images/im/push_fcm_newcertificate_upload.png)

    4. Click **OK** to complete the switch.

    ### 3. Integrate FCM on the client [#3-integrate-fcm-on-the-client-1]

    Take the following steps to integrate FCM on the client.

    1. Install `firebase_core`: Execute `flutter pub add firebase_core` in the project root directory.
    2. Install `firebase_messaging`: Execute `flutter pub add firebase_messaging` in the project root directory.

    **For Android**

    In your flutter project's `Android/build.gradle` file, check the `google-services` version and make sure it is 4.3.14 or above.

    ```dart
    dependencies {
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.3.15'
        // END: FlutterFire Configuration
    }
    ```

    **For iOS**

    Before your app can start receiving messages, enable push notifications and background modes in your Xcode project.

    1. Open the Xcode project workspace (`ios/Runner.xcworkspace`).
    2. [Enable push notifications](https://help.apple.com/xcode/mac/current/#/devdfd3d04a1).
    3. Enable background fetching and remote notifications [background execution modes](https://developer.apple.com/documentation/xcode/configuring-background-execution-modes).

    #### SDK integration [#sdk-integration]

    1. Initialize the Chat SDK and enable FCM:

       ```dart
       void main() async {
         WidgetsFlutterBinding.ensureInitialized();
         // Replace with your Appkey.
         var options = ChatOptions(appKey: "Your Appkey", autoLogin: false);
         // Replace with your FCM Sender ID.
         options.enableFCM("Your FCM sender id");
         // Initialize the IM SDK.
         await ChatClient.getInstance.init(options);
         // Initialize FCM SDK.
         await Firebase.initializeApp(
           options: DefaultFirebaseOptions.currentPlatform,
         );
         runApp(const MyApp());
       }
       ```

    2. Pass the FCM device token to the server.

       When the app is initialized, the FCM SDK generates a unique registration token for the client app on the user's device. Since FCM uses this token to determine which device to send the push message to, the Agora server needs to obtain the client app's registration token before sending the notification request to FCM. FCM then verifies the registration token and sends the notification message to the device.

       ```dart
       // Request push permission
       await FirebaseMessaging.instance.requestPermission();
       FirebaseMessaging.instance.onTokenRefresh.listen((event) async {
           debugPrint('onTokenRefresh: $event');
           await ChatClient.getInstance.pushManager.updateFCMPushToken(event);
       });
       // Get the token and upload it to instant communication.
       final fcmToken = await FirebaseMessaging.instance.getToken();
       if (fcmToken != null) {
         debugPrint('fcmToken: $fcmToken');
         await ChatClient.getInstance.pushManager.updateFCMPushToken(fcmToken);
       } else {
         debugPrint('fcmToken is null');
       }
       ```

    ## Test FCM push [#test-fcm-push-1]

    After integrating and enabling FCM, you can test whether the push feature is successfully integrated.

    Make sure your test device meets the following conditions:

    * Uses foreign IP addresses to establish connections.
    * Supports Google GMS services (Google Mobile Services).
    * Can access Google network services; otherwise, the device won't be able to receive push notifications from the FCM service.

    For more reliable testing, use a physical device.

    ### Test push notifications [#test-push-notifications-2]

    1. Log in to the app on your device and confirm that the device token is successfully bound.

       You can check the log or call [RESTful API for getting user details](..//en/api-reference/api-ref/im/user-system-registration#querying-a-user) to confirm whether the device token is successfully bound. If successful, there will be a `pushInfo` field under the `entities` field, and `pushInfo` will have relevant information such as `device_Id`, `device_token`, `notifier_name`, and others.

    2. Enable app notification bar permissions.

    3. Kill the application process.

    4. Send a test message in the [Agora Console](https://console.agora.io/v2).

       Select **Operation Management** > **User** in the left navigation bar. On the **Users** page, select **Send Admin Message** in the **Action** column for the corresponding user ID. In the dialog box that pops up, select the message type, enter the message content, and then click **Send** .

       <CalloutContainer type="info">
         <CalloutDescription>
           In the **Push Certificate** page, in the **Action** column of each certificate, click **More** and **Test** will appear. This is to directly call the third-party interface to push. The message sending test in the **Users** page first calls the Chat message sending interface and then the third-party interface when the conditions are met (that is, the user is offline, the push certificate is valid and bound to the device token).
         </CalloutDescription>
       </CalloutContainer>

    5. Check your device to see if it has received the push notification.

    ### Troubleshooting [#troubleshooting-2]

    In case of issues, take the following steps:

    1. Check whether FCM push is correctly integrated or enabled:

       Select **Operation Management** > **User** in the left navigation bar. On the **User Management** page, select **Push Certificate** in the **Action** column for the corresponding user ID. In the pop-up box, check whether the certificate name and device token are displayed correctly.
    2. Check whether the correct FCM certificate is uploaded in the [Agora Console](https://console.agora.io/v2).
    3. Check whether the message is pushed in the chat room. The chat room does not support offline message push.
    4. Check if the device supports GMS.
    5. Check whether online-only delivery is enabled (`deliverOnlineOnly` = `true`). Messages set to be delivered only when the receiver is online will not be pushed.

    
  
      
  
