Google Firebase FirebaseMessaging.onMessage.listen 在 flutter 中多次调用

发布于 2025-01-16 04:03:13 字数 6698 浏览 2 评论 0原文

我正在使用 Google FCM 从后端获取通知,在某些情况下,我多次被调用 FirebaseMessaging.onMessage.listen 但经过一些研究后,我发现存在问题

错误

在搜索解决方案后我别无选择,唯一的选择就是执行此类操作解决了

  if ((DateTime.now().microsecondsSinceEpoch - _lastOnResumeCall) >
                  7000000 &&
              _lastUniqId != message.data['uniqueId'].toString()) {
       _lastUniqId = message.data['uniqueId'].toString();
            _lastOnResumeCall = DateTime.now().microsecondsSinceEpoch;
      //showing local notification if condition is true

}

,但问题仍然存在,是否有任何可以实施的解决方案或解决方法?

Flutter doctor结果

当前使用的 Firebase 版本

这是我当前使用的版本

更新

    // ignore_for_file: avoid_print

import 'dart:io';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:project/api/login_request.dart';
import 'package:project/controller/dashboard.dart';
// import 'package:project/models/remote_message.dart';
import 'package:project/utils/global.dart';
import 'package:project/utils/routes.dart';

class FCMProvider {
  static bool gotNewNotification = false;

  DashBoardController conttroller = Get.put(DashBoardController());
  FirebaseMessaging messaging = FirebaseMessaging.instance;
  String? token;
  String? notificationId;
  GlobalFunctions gf = GlobalFunctions();
  String accessToken = "";
  String userRoll = "0";
  String _lastUniqId = "";
  int counte = 0;
  int _lastOnResumeCall = 0;
  int _lastOnResumeCall2 = 0;
  String _lastIdFromshowMessage = "";
  int smearphone = 0;
  UserAPIRepository userAPIRepository = UserAPIRepository();

  stop() {
    messaging.unsubscribeFromTopic("all");
  }

  start() {
    if (accessToken.length > 1) messaging.subscribeToTopic("all");
  }

  initialize() async {
    accessToken = await gf.getToken();
    userRoll = await gf.getRoleId();

    NotificationSettings settings = await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    token = await messaging.getToken();

    await userAPIRepository.setFirebaseToken(token);
    messaging.setForegroundNotificationPresentationOptions(
      alert: Platform.isAndroid,
      badge: true,
      sound: true,
    );

    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    await FirebaseMessaging.instance.getToken();

    FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) async {
        if (message.notification != null) {
          // // conttroller.changeTabIndex(2);
          // // (DateTime.now().microsecondsSinceEpoch - _lastOnResumeCall) >10000000 &&
          // if (_lastUniqId.toString() != message.data['uniqueId'].toString()) {

          // }
          if (smearphone != 0) {
            return;
          }
          smearphone = 1;
          Future.delayed(const Duration(seconds: 1), () {
            smearphone = 0;
          });
          String oldunqi = _lastUniqId;
          _lastUniqId = message.data['uniqueId'].toString();

          showNotification(
            "Last Unqie Id " + oldunqi.toString(),
            "Recived Unqiue Id" + _lastUniqId.toString(),
            _lastUniqId.toString(),
          );
        }
      },
    );

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
      if (accessToken.length > 1) {
        if (message.data.containsKey("notificationType")) {
          if (message.data["notificationType"] == "Message") {
            if (userRoll == "5") {
              conttroller.changeTabIndex(2);
            } else {
              conttroller.changeTabIndex(1);
            }
          } else {
            conttroller.changeTabIndex(0);
          }
        }
        // showNotification(message.notification!.title.toString(),
        //     message.notification!.body.toString());
        FCMProvider.gotNewNotification = true;
        Get.toNamed(Routes.splash);
      }
    });

    start();
  }

  showNotification(String title, String message, id, {int type = 0}) {
    // Get.dialog(
    //   AlertDialog(
    //     title: Text(title),
    //     content: Text(message),
    //     actions: <Widget>[
    //       FlatButton(
    //         child: Text("Ok"),
    //         onPressed: () {
    //           Get.back();
    //         },
    //       )
    //     ],
    //   ),
    // );
    Get.showSnackbar(GetSnackBar(
      borderRadius: 10,
      maxWidth: 250,
      margin: const EdgeInsets.only(bottom: 10),
      duration: const Duration(seconds: 5),
      messageText: Text(
        "Varibel checking" +
            (_lastIdFromshowMessage.toString() != id.toString()).toString() +
            " New id" +
            id.toString() +
            " Last Id" +
            _lastIdFromshowMessage.toString(),
        style: const TextStyle(color: Colors.white),
      ),
    ));
    if (_lastIdFromshowMessage.toString() != id.toString()) {
      _lastIdFromshowMessage = id.toString();
      Get.snackbar(
        title,
        message,
        duration: const Duration(seconds: 5),
        dismissDirection: DismissDirection.horizontal,
        boxShadows: const [
          BoxShadow(
            color: Colors.white,
            offset: Offset(0, 0),
          ),
        ],
        colorText: Colors.black, //kGreen,
      );
    }
  }
}

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print(message.data);
  print("Handling a background message: ${message.messageId}");
}

这是推送通知类

import 'package:portal_del_familiar/controller/bindings/dashboardbind.dart';

import 'package:portal_del_familiar/ui/my_app.dart';
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await GetStorage.init();
  DashboardBinding().dependencies();

  runApp(const MyApp());
}

Main.dart

如您所见,我正在检查另一个解决方案,如果有任何更改,我也会更新。我已经检查过变量没有更新,因为消息同时发送,如果您能提供一种在下一次调用之前更新变量的方法,我真的很感激。

I am using Google FCM to get notification from back end there are some situation where i am getting called FirebaseMessaging.onMessage.listen multiple times but after doing some research i have found that there is an issue

bug

after searching for a solution i had no option only option was to do this type of work around

  if ((DateTime.now().microsecondsSinceEpoch - _lastOnResumeCall) >
                  7000000 &&
              _lastUniqId != message.data['uniqueId'].toString()) {
       _lastUniqId = message.data['uniqueId'].toString();
            _lastOnResumeCall = DateTime.now().microsecondsSinceEpoch;
      //showing local notification if condition is true

}

but the issue is still remains is there any solution or work around that can be implemented?

Flutter doctor Result

Firebase version that currently using

This is are the version that i am using currently

Update

    // ignore_for_file: avoid_print

import 'dart:io';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:project/api/login_request.dart';
import 'package:project/controller/dashboard.dart';
// import 'package:project/models/remote_message.dart';
import 'package:project/utils/global.dart';
import 'package:project/utils/routes.dart';

class FCMProvider {
  static bool gotNewNotification = false;

  DashBoardController conttroller = Get.put(DashBoardController());
  FirebaseMessaging messaging = FirebaseMessaging.instance;
  String? token;
  String? notificationId;
  GlobalFunctions gf = GlobalFunctions();
  String accessToken = "";
  String userRoll = "0";
  String _lastUniqId = "";
  int counte = 0;
  int _lastOnResumeCall = 0;
  int _lastOnResumeCall2 = 0;
  String _lastIdFromshowMessage = "";
  int smearphone = 0;
  UserAPIRepository userAPIRepository = UserAPIRepository();

  stop() {
    messaging.unsubscribeFromTopic("all");
  }

  start() {
    if (accessToken.length > 1) messaging.subscribeToTopic("all");
  }

  initialize() async {
    accessToken = await gf.getToken();
    userRoll = await gf.getRoleId();

    NotificationSettings settings = await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    token = await messaging.getToken();

    await userAPIRepository.setFirebaseToken(token);
    messaging.setForegroundNotificationPresentationOptions(
      alert: Platform.isAndroid,
      badge: true,
      sound: true,
    );

    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    await FirebaseMessaging.instance.getToken();

    FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) async {
        if (message.notification != null) {
          // // conttroller.changeTabIndex(2);
          // // (DateTime.now().microsecondsSinceEpoch - _lastOnResumeCall) >10000000 &&
          // if (_lastUniqId.toString() != message.data['uniqueId'].toString()) {

          // }
          if (smearphone != 0) {
            return;
          }
          smearphone = 1;
          Future.delayed(const Duration(seconds: 1), () {
            smearphone = 0;
          });
          String oldunqi = _lastUniqId;
          _lastUniqId = message.data['uniqueId'].toString();

          showNotification(
            "Last Unqie Id " + oldunqi.toString(),
            "Recived Unqiue Id" + _lastUniqId.toString(),
            _lastUniqId.toString(),
          );
        }
      },
    );

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
      if (accessToken.length > 1) {
        if (message.data.containsKey("notificationType")) {
          if (message.data["notificationType"] == "Message") {
            if (userRoll == "5") {
              conttroller.changeTabIndex(2);
            } else {
              conttroller.changeTabIndex(1);
            }
          } else {
            conttroller.changeTabIndex(0);
          }
        }
        // showNotification(message.notification!.title.toString(),
        //     message.notification!.body.toString());
        FCMProvider.gotNewNotification = true;
        Get.toNamed(Routes.splash);
      }
    });

    start();
  }

  showNotification(String title, String message, id, {int type = 0}) {
    // Get.dialog(
    //   AlertDialog(
    //     title: Text(title),
    //     content: Text(message),
    //     actions: <Widget>[
    //       FlatButton(
    //         child: Text("Ok"),
    //         onPressed: () {
    //           Get.back();
    //         },
    //       )
    //     ],
    //   ),
    // );
    Get.showSnackbar(GetSnackBar(
      borderRadius: 10,
      maxWidth: 250,
      margin: const EdgeInsets.only(bottom: 10),
      duration: const Duration(seconds: 5),
      messageText: Text(
        "Varibel checking" +
            (_lastIdFromshowMessage.toString() != id.toString()).toString() +
            " New id" +
            id.toString() +
            " Last Id" +
            _lastIdFromshowMessage.toString(),
        style: const TextStyle(color: Colors.white),
      ),
    ));
    if (_lastIdFromshowMessage.toString() != id.toString()) {
      _lastIdFromshowMessage = id.toString();
      Get.snackbar(
        title,
        message,
        duration: const Duration(seconds: 5),
        dismissDirection: DismissDirection.horizontal,
        boxShadows: const [
          BoxShadow(
            color: Colors.white,
            offset: Offset(0, 0),
          ),
        ],
        colorText: Colors.black, //kGreen,
      );
    }
  }
}

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print(message.data);
  print("Handling a background message: ${message.messageId}");
}

This is pushnotification class

import 'package:portal_del_familiar/controller/bindings/dashboardbind.dart';

import 'package:portal_del_familiar/ui/my_app.dart';
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await GetStorage.init();
  DashboardBinding().dependencies();

  runApp(const MyApp());
}

Main.dart

As you can see i am checking another solution also i will update if there is any changes. and i have checked the variables are not updating in condition since the messages are reeving concurrently really appreciate if you can give a way to update the variable before next call is come.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2025-01-23 04:03:13

好吧,现在我知道这有点蹩脚,但我能够解决这个问题。原因是曾经做过代码的人在 3 个地方调用 Firebase Messaging init 函数,其中一个在启动画面中第二次登录成功,最后在登录时退出,所以每次它创建一个实例时,这样首先它会在启动画面中创建一个实例,然后在登录(即make 2)然后当用户注销并登录时,它会创建另一个实例,因此每次它都会增加一个侦听器,这就是原因,因此对于我的解决方案,我将 init 放置在 main() 中,并从其他所有地方删除,这解决了这个问题。 @Uni Gandhi 非常感谢您那天的帮助。

Okay now i know this is bit lame but i was able to fix the issue. The reason was who ever done code was calling Firebase Messaging init function in 3 places one in Splash screen second login success and in the end when login out so every time it was creating a instance so that way first it's create a instance in splash then in login (that's make 2) then when user logged out and login it's create another instance so every time it's increase one listener so this was the reason so for my solution i have placed init in the main() and removed from every other place and this fixed the issue. @Uni Gandhi Thank you very much for you help on that day.

那些过往 2025-01-23 04:03:13

我的情况没有发生。我只收到 1 条通知消息。

您可以尝试使用简单的整数来实现另一种解决方法,并根据奇/偶值显示消息。

如果您可以分享您的 Firebase 初始化和 OnMessage 代码,那么可以提供更好的解决方案。

Not happening in my case. I am getting only 1 notification message.

You could try implementing another workaround using a simple integer and displaying the message based on odd/even values.

If you could share your Firebase initialization and OnMessage code then a better solution can be provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文