我正在使用 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
}
但问题仍然存在,是否有任何可以实现的解决方案或变通办法?
这是我当前使用的版本
更新
// 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
正如您所看到的,我正在检查另一个解决方案,如果有任何更改,我也会更新。我已经检查了变量没有更新,因为消息同时发送,如果您能提供一种在下一次调用之前更新变量的方法,我真的很感激。
最佳答案
好吧,现在我知道这有点蹩脚,但我能够解决这个问题。原因是曾经做过代码的人在 3 个地方调用 Firebase Messaging init 函数,其中一个在启动画面第二次登录成功,最后在注销时,所以每次它创建一个实例时,这样首先它在启动画面中创建一个实例,然后在登录(即make 2)然后当用户注销并登录时,它会创建另一个实例,因此每次它都会增加一个监听器,这就是原因,因此对于我的解决方案,我将 init 放置在 main() 中,并从其他所有地方删除,这解决了这个问题。 @Uni Gandhi 非常感谢您那天的帮助。
关于android - Google Firebase FirebaseMessaging.onMessage.listen 在 flutter 中多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71582305/