带有飞镖的颤振 FCM

时间:2021-04-29 08:01:40

标签: flutter

我想在用户点击按钮时发送 FCM。我正在尝试使用 subscribeToken 发送消息,但我没有使用过 Node.js,所以我不知道如何处理 Firebase 函数。因此我想使用 dart 语言发送它。有没有什么办法用dart语言发送FCM?

await http.post(
    'http://fcm.googleapis.com/fcm/send',
    headers: <String, String>{
      'Content-Type': 'application/json',
      'Authorization': 'key=$serverToken',
    },
    body: jsonEncode(
      <String, dynamic>{
        'notification': <String, dynamic>{
          'body': 'this is a body',
          'title': 'this is a title',
        },
        'priority': 'high',
        'data': <String, dynamic>{
          'click_action': 'FLUTTER_NOTIFICATION_CLICK',
          'id': '1',
          'status': 'done'
        },
        'to': widget.gallery.author + widget.gallery.reference.id + 'like',
      },
    ),
  );

2 个答案:

答案 0 :(得分:0)

如果没有用作 firebase 管理员的服务帐户,您将无法发送 FCM,因此您可以使其工作的唯一方法是通过 firebase 管理员,而最简单的方法是使用 firebase 功能。您可以在 youtube 上观看有关如何仅为 FCM 进行配置的视频

答案 1 :(得分:0)

可以使用 dart 发送通知,但只能直接从您的 Flutter 应用程序发送。因为 Firebase Cloud Functions 没有 dart 实现。

为此,您首先必须将 http 作为依赖项添加到您的 pubspec.yaml 文件中。
安装方法:https://pub.dev/packages/http/install

然后创建一个 credentials.dart
现在,转到:https://console.firebase.google.com/u/0/project/YOUR_PROJECT_ID/settings/cloudmessaging
并复制Server key。 (在 URL 中将 YOUR_PROJECT_ID 替换为您的实际 Firebase 项目 ID)
credentials.dart 文件中写入:

const FCM_SERVER_KEY = ''; /// Put the Server key you copied here.

credentials.dart 文件应该是私有的,您不应上传到任何源代码管理。

现在使用这个辅助类访问 Google 的 FCM api:

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:path/to/credentials.dart' as credentials;

class Notifications {
  /// Handles sending FCM notifications
  /// using Google's FCM api.
  static Notifications get instance => Notifications();
  static const Map<String, dynamic> DEFAULT_NOTIFICATION_DATA = {
    'click_action': 'FLUTTER_NOTIFICATION_CLICK',
    'type': 'default',
  };

  Future<String> send(
    String fcmToken, {
    String title,
    String body,
    Map<String, dynamic> data = DEFAULT_NOTIFICATION_DATA,
  }) async {
    /// Sends a notification with the
    /// given title and body to the given
    /// FCM token.
    try {
      http.Response r = await http.post(
        'https://fcm.googleapis.com/fcm/send',
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization': 'key=${credentials.FCM_SERVER_KEY}',
        },
        body: jsonEncode(
          <String, dynamic>{
            'notification': <String, dynamic>{
              'title': title,
              'body': body,
              'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            },
            'priority': 'high',
            'data': data,
            'to': fcmToken,
          },
        ),
      );

      return r.body;
    } catch (e) {
      return e.toString();
    }
  }
}

发送通知:

import 'package:path/to/notifications.dart';
final String fcmToken = '';
Notifications.instance.send(fcmToken);
/// fcmToken is the FCM token of the device you want to send the notification to 

如何获得 fcmToken?

根据您使用的 Firebase Messaging 版本,您可以通过调用以下方法获取用户设备的消息令牌:

String fcmToken = await FirebaseMessaging.instance.getToken();

现在,当您调用 Notifications.send(fcmToken); 函数时,与 fcmToken 关联的设备将收到您的通知。 在您的情况下,您可以通过为函数提供 fcmToken 来将通知发送到图库所有者的设备。

我建议您将这些令牌存储在数据库中。但是,请记住,如果用户在其他设备上登录他们的帐户,他们的令牌将会更改,您必须对其进行更新。