【问题标题】:Send notifications using REST api使用 REST api 发送通知
【发布时间】:2021-08-17 09:02:43
【问题描述】:

我想使用http包通过flutter应用程序使用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"
      }
   }
}

因为它需要每小时过期的访问令牌。我如何在颤振中管理它?

【问题讨论】:

  • 是否需要http 包?否则,您可能会查看颤振包。他们做了很多繁重的工作并处理诸如刷新令牌之类的事情:firebase.flutter.dev/docs/messaging/overview
  • 我应该如何在没有 http 包的情况下向 REST API 发送请求?是的,我正在使用该插件,但该插件能够使用他的令牌向特定用户发送通知。我想要的是向主题的订阅者发送通知。 Node.js 有可用的 admin sdk,它可以轻松管理 Oauth 密钥,但我没有发现 Flutter。
  • 另一种解决方法是使用旧版 FCM API。
  • 是的,我目前正在使用它,但我想迁移到新的 API

标签: firebase flutter dart


【解决方案1】:

终于找到了使用google_sign_in包获取OAuth代码的方法。

  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;
  }

【讨论】:

    猜你喜欢
    • 2016-09-26
    • 1970-01-01
    • 2018-12-31
    • 2021-10-17
    • 2015-05-18
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多