FCM不用firbase_messgage在颤音中工作: ^11.4.2

发布于 2025-02-11 01:19:09 字数 2363 浏览 1 评论 0原文

fcm不用firbase_messaging在颤音中工作: ^11.4.2

我尝试使用旧数据和新的数据有效载荷,以实现FCM,但当我们手动从Firebase发送通知时。它来了,击中了本地广播的接收器, 消息显示,但是当我们按代码和响应发送时,我们会得到200,这很好,但是插件中的问题在于,该插件没有按时广播接收器,因此该消息不显示。

//<--Here -- sendPushMessageRun Here-->
  Future<void> sendPushMessage() async  {
    if (_token == null) {
      print('Unable to send FCM message, no token exists.');
      return;
    }

    try {
      final response = await http.post(
        Uri.parse('https://api.rnfirebase.io/messaging/send'),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': 'key=A....Q'
        },
        body: constructFCMPayload(_token),
      );
      if(response.statusCode == 200){
        print('Done!');
       await Firebase.initializeApp();
        ///
       await FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          RemoteNotification? notification = message.notification;
          AndroidNotification? android = message.notification?.android;
          if (notification != null && android != null && !kIsWeb) {
            flutterLocalNotificationsPlugin.show(
              notification.hashCode,
              notification.title,
              notification.body,
              NotificationDetails(
                android: AndroidNotificationDetails(
                  channel.id,
                  channel.name,
                  channelDescription:channel.description,
                  // TODO add a proper drawable resource to android, for now using
                  //      one that already exists in example app.
                  icon: 'launch_background',
                ),
              ),
            );
          }
        });

      }else{
        print('Not Done!'+response.statusCode.toString());
      }

    } catch (e) {
      print(e);
    }
  }
String constructFCMPayload(String? token) {
  _messageCount++;
  return jsonEncode({
    'topic': "fcm_test", //also try with "to" : "/topics/fcm_test"
    'data': {
      'via': 'FlutterFire Cloud Messaging!!!',
      'count': _messageCount.toString(),
    },
    'notification': {
      'title': 'Hello FlutterFire!',
      'body': 'This notification (#$_messageCount) was created via FCM!',
    },
  });`enter code here`
}

FCM Not working in flutter with firebase_messaging: ^11.4.2

I have try with both old and new data payload for implementing FCM with Topic but when we send the notification from firebase manually . It comes and hit the natively broadcast receiver and
message shows but when we send by code and response we get 200 which is fine but the problem in plugin is that is not hit that time broadcast receiver so that message not show.

//<--Here -- sendPushMessageRun Here-->
  Future<void> sendPushMessage() async  {
    if (_token == null) {
      print('Unable to send FCM message, no token exists.');
      return;
    }

    try {
      final response = await http.post(
        Uri.parse('https://api.rnfirebase.io/messaging/send'),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': 'key=A....Q'
        },
        body: constructFCMPayload(_token),
      );
      if(response.statusCode == 200){
        print('Done!');
       await Firebase.initializeApp();
        ///
       await FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          RemoteNotification? notification = message.notification;
          AndroidNotification? android = message.notification?.android;
          if (notification != null && android != null && !kIsWeb) {
            flutterLocalNotificationsPlugin.show(
              notification.hashCode,
              notification.title,
              notification.body,
              NotificationDetails(
                android: AndroidNotificationDetails(
                  channel.id,
                  channel.name,
                  channelDescription:channel.description,
                  // TODO add a proper drawable resource to android, for now using
                  //      one that already exists in example app.
                  icon: 'launch_background',
                ),
              ),
            );
          }
        });

      }else{
        print('Not Done!'+response.statusCode.toString());
      }

    } catch (e) {
      print(e);
    }
  }
String constructFCMPayload(String? token) {
  _messageCount++;
  return jsonEncode({
    'topic': "fcm_test", //also try with "to" : "/topics/fcm_test"
    'data': {
      'via': 'FlutterFire Cloud Messaging!!!',
      'count': _messageCount.toString(),
    },
    'notification': {
      'title': 'Hello FlutterFire!',
      'body': 'This notification (#$_messageCount) was created via FCM!',
    },
  });`enter code here`
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文