Using Firebase with Batch.com

Using Firebase with Batch

Step 1. Integrating Batch

First, you'll need to integrate Batch in your app. Don't worry, it should take you less than one hour per platform.

Please follow the "Initial Setup" and "Push Setup" tutorials.

And here the docs for iOS:

Here are their Android counterparts:

If your app already has push notifications, and you have your own FCM Service Account Key for Android, you can skip "Prerequisites"

Step 2. Integrating Firebase

In order to push your users with Firebase, you will need to implement their login system.

Here is their iOS documentation:

Here is the Android documentation:

Even if your app doesn't have a login system, we advise you silently log him in using an "anonymous" login.

Step 3. Making Batch and Firebase work together

Here's the magic part: You may not know it yet, but you can give a custom user identifier (iOS/Android) to Batch SDK. It allows you to push users using our Transactional REST API with this identifier rather than raw tokens.

We'll handle the tokens and all of their complexity for you: multiple devices, token changes...

Custom user ID

Once you've authentified with Firebase, they will give you an object describing the auth data (AuthData on Android, FAuthData on iOS).

This object gives you an uid property, which is unique for this user, even across all authentication providers! That makes it a perfect candidate for Batch's custom user identifier: You'll easily be able to query it, and send it to our REST API.

Here's how to give it to Batch on Android:

// firebaseAuthData is populated by the argument of "onAuthenticated" in  "authAnonymously" / "authWithOAuthToken"

AuthData firebaseAuthData = ...;
Batch.User.getEditor()
  .setIdentifier(firebaseAuthData.getUid())
  .save(); // Don't forget to save the changes!

And here's how to do it on iOS:

// firebaseAuthData is populated by the result of "authAnonymouslyWithCompletionBlock" / "authWithOAuthProvider"

FAuthData *firebaseAuthData = ...;

BatchUserDataEditor *editor = [BatchUser editor];
[editor setIdentifier:firebaseAuthData.uid];
[editor save]; // Do not forget to save the changes!
// firebaseAuthData is populated by the result of "authAnonymouslyWithCompletionBlock" / "authWithOAuthProvider"
let firebaseAuthData: FAuthData = ...

let editor = BatchUser.editor()
editor.setIdentifier(firebaseAuthData.uid)
editor.save() // Do not forget to save the changes!

You're done! Now head back to the tutorial you're coming from. We're going to send your first Push with Firebase!

Sample App

Do you believe code is better than words?

We've got you covered, here's a sample app that shows how you can integrate Batch and Firebase: https://github.com/BatchLabs/firebase-batch-demo