Creating Topics for Firebase Cloud Messaging using Postman (Android)

Topic Messaging is one method you can use Firebase cloud messaging to distribute information. This use publish/subscribe model where multiple devices can subscribe to a particular topic to receive messages. Today we are going to discuss how to create FCM topics using Postman (an API test tool)

Open the Postman. You will see a window like below.

Now we need to collect other Information to create correct post request.

First of all, we need to authorize our request from Firebase. For that, we need to send authorization details in Http header of our request. Firebase authorization can be done in two ways.

  1. short-lived OAuth 2.0 access token generated for the service account
  2. long-lived API keys

In this discussion, we use long-lived API key from Firebase. You can get your Server key from Firebase Console Click the link and choose your firebase project, Link will bring you there. If you don’t have one create a new project.

Now We have Server key. next, we need is Firebase Instant ID. (This is hoping that you have integrated firebase into your Android project) Below is the piece of code that you need to get the Instant id from the app.

String token = FirebaseInstanceId.getInstance().getToken();

The easiest way to get token (As my idea) is to write a log with the token for a button click.

Let say we add a button for the token in any activity(Ex – MainActivity) sample java could be similar to below.

Button tokenButton = findViewById(R.id.tokenButton);

tokenButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

String token = FirebaseInstanceId.getInstance().getToken();

Log.i("Token", "Token = " +token)

You can get your IID token in Logcat like below. (Since I add “Token” tag in there I can search it like below)

 

copy the IID_TOKEN from logcat. Now you are ready to send the response. Below is the example URL for Firebase Cloud Messaging topics creating.

https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME

TOPIC_NAME should be the topic you need to create. Now you have IID_TOKEN and TOPIC_NAME you need to create. Let’s add them in postman like below.

Of course, you will need to add long IID_TOKEN in the url, not like above 😀 Now we need to add authorization to the HTTP request. Select Headers section below the URL adds key and value to respective fields.

Key= “Authorization”  Value = “key=YOUR_API_KEY”

Key= “Content-Type”  Value =  “application/json”

Here you don’t need to add double quotations (“”) it just specifies.  we used Content-Type: application/json to specify that we are expecting a json. Your Headers now should look like below.

Remember to replace the value of YOUR_API_KEY with Server key that we got before. Now we are ready to create Firebase Cloud Messaging Topic. Press Send Button. If the request was a success you will get empty json, otherwise, you will get json with an error message in it.

If you want to see that Topic was created or not send HTTP request like below.

https://iid.googleapis.com/iid/info/IID_TOKEN?details=true

Remember to replace IID_TOKEN with your token. Then include that header information like before and send the request. On successful authentication, you will receive JSON Topic details.

About Author:

3 thoughts on “Creating Topics for Firebase Cloud Messaging using Postman (Android)

Leave A Comment

Your email address will not be published. Required fields are marked *