Skip to main content
All CollectionsUsing the Portal
Push Notification Setup – Firebase
Push Notification Setup – Firebase

Firebase Cloud Messaging (FCM), Android

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

This tutorial will walk you through the steps to configure Firebase Cloud Messaging (FCM) with brainCloud.

Prerequisites

Step1: Configure Firebase Cloud Messaging API (V1) private key JSON to brainCloud

  • Navigate your browser to the Firebase console and select your designated project. Click the Settings Icon -> “Project settings” in the top left navbar.

  • Select the “Cloud Messaging” tab and make sure the "Firebase Cloud Messaging API" is enabled for your Firebase project in the Google Cloud Platform.

  • If not, you can always go to your GCP project and enable it from the API Library.

  • Switch to the "Service accounts" tab on the same project settings page, and click the "Generate new private key" button on the screen to generate a private key.

  • Once the new private key is generated, you can see it shown on the linked service account in GCP.

  • Open this generated JSON file with an editor and copy the entire JSON content.

  • Navigate to the brainCloud development console Design | Notification | Settings page, select Google Play, and click Edit Settings.

  • Click the Edit button on the pop-up window, then paste the JSON content into the "FCM Server Account JSON" input box, then save it.

Step2: Register a notification token to your device via your app

  • Use the Keystore file of your Android app to create a fingerprint SHA1 and add it to your Firebase project.

  • Download google-services.json file and add it to your app root folder (for the Unity app, it will be under Assets folder). This file contains most of the credentials you’ll need to connect your app to Firebase.

  • Follow the SDK Instruction to finish settings for your app. (If you are using the Unity Firebase Messaging package, after importing, your Assets folder should be similar to the structure below)

  • In your app, after authenticating a user with brainCloud, you will need to register the Firebase Registration token with brainCloud. Specifically, you’ll want to call the RegisterPushNotificationDeviceToken() method to pass in the token.

  • If you are using Unity SDK, your code should be similar to the following.

        void InitializeFirebase()
    {
    Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
    Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    Firebase.Messaging.FirebaseMessaging.SubscribeAsync(topic).ContinueWithOnMainThread(task => {
    LogTaskCompletion(task, "SubscribeAsync");
    });
    DebugLog("Firebase Messaging Initialized");
    Firebase.Messaging.FirebaseMessaging.RequestPermissionAsync().ContinueWithOnMainThread(
    task => {
    LogTaskCompletion(task, "RequestPermissionAsync");
    }
    );
    isFirebaseInitialized = true;
    }
    ...
    public virtual void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    {
    DebugLog("Received Registration Token: " + token.Token);
    firebaseToken = token.Token;
    AddStatusText("Received Registration Token: " + token.Token);
    }
    ...
    public void OnRegisterToken()
    {
    _bc.PushNotificationService.RegisterPushNotificationDeviceToken(Platform.GooglePlayAndroid, firebaseToken, authSuccess_BCcall, authError_BCcall);
    }
    ...

  • If using Java Android studio

      FirebaseInstanceId.getInstance().getInstanceId()
    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
    @Override
    public void onComplete(@NonNull Task<InstanceIdResult> task) {
    if (!task.isSuccessful()) {
    Log.w(TAG, "getInstanceId failed", task.getException());
    return;
    }
    // Get new Instance ID token
    String token = task.getResult().getToken();
    _bc.GetWrapper().getPushNotificationService().registerPushNotificationToken(Platform.GooglePlayAndroid, token, theCallback);
    }
    }); FirebaseInstanceId.getInstance().getInstanceId()

Step3: Test

  • Run your app, authenticate an end-user to brainCloud, and call RegisterPushNotificationDeviceToken() method to register the device token.

  • Once your test device has registered the notification token to brainClound from your app, you can check it from the User Monitoring | User Summary page.

  • Hit Send Notification button from the above page will pop up a Send Notification window, fill some text, and Click Send. Your test device should receive this notification from brainCloud. You can always use brainCloud Push Notification methods to test push notifications too.

  • Note that the above example is the settings for Android, however, the configuration steps are almost the same, for details of setting up iOS with FCM, please visit Firebase documentation.

Did this answer your question?