繁体   English   中英

Firebase Cloud Messaging 不适用于使用 Python 以编程方式发送的消息

[英]Firebase Cloud Messaging not working for programmatically sent messages using Python

如果我 go 进入我的 firebase 控制台并设置活动,我的终端设备会很好地收到通知,但是对于使用设备的注册令牌从 django/python 发送到特定设备的消息,我的移动设备上没有收到通知。

不确定这是否重要,但我的应用程序仍在开发中,尚未投入生产,所以如果这很重要,请告诉我。

我的前端是 flutter,这是我用来获取注册令牌并将其发送到后端的 flutter 代码:

Future<StreamedResponse> AddProductPut(context, pk, name, quantity, cost, selling, XFile? chosenImage) async {
  String id_token = await FirebaseAuth.instance.currentUser!.getIdToken();
  late String? fcm_token;
  await FirebaseMessaging.instance.getToken().then((token) async {
    fcm_token = token!;
  }).catchError((e) {
    print(e);
  });
  print(fcm_token);
  var url = backend + "/pm/createproduct/" + pk.toString() + "/";
  var request = http.MultipartRequest('PUT', Uri.parse(url));
  print("FCM TOKEN");
  print(fcm_token);
  request.headers["Authorization"] = "Token " + id_token;
  request.fields["name"] = name;
  request.fields["quantity"] = quantity.toString();
  request.fields["cost_price"] = cost.toString();
  request.fields["selling_price"] = selling.toString();
  request.fields["barcode"] = "11111";
  request.fields["token"] = fcm_token!;
  request.files.add(
      await http.MultipartFile.fromPath(
          'image',
          chosenImage!.path
      )
  );

  return await request.send();
}

这是我的 django 序列化程序中用于发送通知消息的 python 代码:

registration_token = self.context['request'].data["token"],
        print(registration_token[0])
        print(type(registration_token[0]))
        # See documentation on defining a message payload.
        message = Message(
            notification=Notification(
                title='New Product Added',
                body='A new product called ' + validated_data['name'] + ' has been added to your account.',
            ),
            token=registration_token[0]
        )

        # Send a message to the device corresponding to the provided
        # registration token.
        response = send(message)
        print(response)

发送此消息时,我的 django/python 控制台没有收到任何错误,并且响应打印如下内容:

projects/<project name>/messages/16641.......0329

我的注册令牌是这样的:

cmLYCAbL0EsJrxppKzXvhF:APA..............qdt5ySYLbkQC_bpqwL6RdCwSzK_tX8iclp-e0QZB................lgw9g2eFNzfXpn2C4U................UnMphyWa6L9d-wUg

不确定是什么问题,但我现在在 Apple 和 Android 上收到 FCM 消息...

            registration_token = subuser.fcm
            print(registration_token)
            message = Message(
                notification=Notification('Added As A Sub-User', f'A user with the phone {instance.userprofile.phone} has added you to their account as a sub user.'),
                token=registration_token
            )

和我的 flutter 代码,它使用用户的 fcm 注册令牌更新数据库......

    FirebaseMessaging.instance.getToken().then((token) async {
      print(token);
      String id_token = await FirebaseAuth.instance.currentUser!.getIdToken();
      String uid = await FirebaseAuth.instance.currentUser!.uid;
      final result = await http.put(
        Uri.parse(backend + "/pm/setfcm/" + uid + "/"),
        headers: {
          "Content-Type": "application/json",
          "Authorization": "Token " + id_token
        },
        body: jsonEncode({
          "fcm": token,
        })
      );
    }).catchError((e) {
      print(e);
    });

    FirebaseMessaging.instance.onTokenRefresh.listen((newToken) async {
      print(newToken);
      String id_token = await FirebaseAuth.instance.currentUser!.getIdToken();
      String uid = await FirebaseAuth.instance.currentUser!.uid;
      final result = await http.put(
          Uri.parse(backend + "/pm/setfcm/" + uid + "/"),
          headers: {
            "Content-Type": "application/json",
            "Authorization": "Token " + id_token
          },
          body: jsonEncode({
            "fcm": newToken,
          })
      );

    });

            response = send(message)
            print(response)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM