Firebase Push Notification From App Server

By | May 6, 2018

You can send message from server to device with firebase “FCM Server”.

First go to firebase console and create new project.

After that you can follow this steps.

Push Notification with Firebase from App Server

For example, you can send notif using http POST like this:

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer {your access token}

{
  "message":{
    "token" : "{FCM Token from your device}",
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM (Firebase Cloud Messaging) Message",
      }
   }
}

Generate private key file:

  1. Go to project settings
  2. Tab “Service Accounts”
  3. Firebase admin SDK
  4. Click generate new private key button
  5. Then use this file to get the bearer access token 🙂

In this case i’m use Java to get bearer access token:

public static String getAccessToken() throws IOException{
        GoogleCredential googleCredential = GoogleCredential
                .fromStream(
                        new FileInputStream("src/main/resources/app-dcf62-firebase-adminsdk-9ixpm-649284f5fa.json")
                )
                .createScoped(
                        Arrays.asList("https://www.googleapis.com/auth/firebase.messaging", "https://www.googleapis.com/auth/firebase.remoteconfig")
                );
        googleCredential.refreshToken();
        return googleCredential.getAccessToken();
    }

Don’t forget to import google api client library to make your life more better :

compile('com.google.api-client:google-api-client:1.23.0')

And to get firebase token from device you can follow me here (next tutorial) if you develop android or IOS using flutter framework.

Note: Before try this, you must setup your android / IOS first to be able retrieve the notification from app server.

Leave a Reply

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