0
votes

I want to use the http package to send notifications to topics through the flutter app using the docs as in https://firebase.google.com/docs/cloud-messaging/send-message#send-messages-to-topics

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

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}

Since it requires Access Token which expires every hour. How do I manage it in flutter?

1
Is the http package required? Otherwise, you might have a look at the flutter packages. They do a lot of the heavy-lifting and handle stuff like refresh of the token: firebase.flutter.dev/docs/messaging/overview - Mark
How am I supposed to send request to REST API without http package? Yes, I am using the plugin but the plugin is capable to send notification to the specific user using his token. What I want is send notifications to the subscribers of the topic. There are admin sdk available for Node.js which easily manages the Oauth key but I found none for flutter. - Kedar Karki
another work around is to use legacy FCM API. - Krish Bhanushali
Yes I am using it currently but I want to migrate to the new API - Kedar Karki

1 Answers

0
votes

Finally I found the way to get the OAuth code using google_sign_in package.

  Future<String?> getAccessToken() async {
    final googleSignIn = GoogleSignIn(
        scopes: ['https://www.googleapis.com/auth/firebase.messaging']);
    final c = await googleSignIn.signIn();
    final a = await c!.authentication;
    return a.accessToken;
  }