This tutorial will walk you through the steps to configure Firebase Cloud Messaging (FCM) with brainCloud.
Prerequisites
Must have created a project on the Firebase console
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 approot
folder (for the Unity app, it will be underAssets
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, yourAssets
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;
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 aSend Notification
window, fill some text, and ClickSend
. 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.