*

Push Notification Setup – Firebase

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

Prerequisites

Step1: Configure FCM cloud messaging server-key to brainCloud

  • Navigate your browser to the Firebase console and select your project. Click the Settings Icon -> “Project settings” in the top left nav bar.
  • Select “Cloud Messaging” and copy the Server Key.
  • Navigate to the brainCloud development console Design | Notification | Settings page, click Edit Settings.
  • Paste this key into Google Play API key field, and set expiration days for this key.

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 Unity will 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 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);
      }
  });

Step3: Test

  • Run your app, authenticate an end-user to brainCloud and call RegisterPushNotificationDeviceToken() method to register device token.
  • Once your test device is 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 detail of setting up iOS with FCM, please visit Firebase documentation.