我已经实现了firebase_messaging颤振包建议的基本配置。但是,每次在我的颤振应用程序onMessage上收到通知时,都会触发两次。我使用的是firebase_messaging 6.0.9、Dart 2.7.0和颤振1.12.13+hotfix.5。
这是我的项目/android/build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}这是我的项目/android/app/build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 28
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.chat_notification"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'这是两次触发onMessage的代码
import 'package:chat_notification/model/message.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
class MessageWidget extends StatefulWidget {
@override
_MessageWidgetState createState() => _MessageWidgetState();
}
class _MessageWidgetState extends State<MessageWidget> {
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
List<Message> messages = [];
@override
void initState() {
// TODO: implement initState
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> response) async {
print("onMessage: $response");
},
onLaunch: (Map<String, dynamic> response) async {
print("onLaunch: $response");
},
onResume: (Map<String, dynamic> response) async {
print("onResume: $response");
},
);
}
@override
Widget build(BuildContext context) => ListView(
children: messages.map(buildMessage).toList(),
);
Widget buildMessage(Message message) => ListTile(
title: Text(message.title),
subtitle: Text(message.body),
);
}我已经尝试过创建新的项目,但似乎每个项目都会出现这种情况。如果有人能帮我解决这件事,我会非常感激的。
编辑:
这在最新版本的firebase_messaging: 7.0.0中不再存在。我找到的最佳解决方案是更新包。复制的消息不再存在。
发布于 2020-01-18 18:27:02
更新:
这在最新版本的firebase_messaging (7.0.0或更高版本)中不再存在。我找到的最佳解决方案是更新包。复制的消息不再存在。
原解决方案:
我也面临着同样的问题。我不知道原因和解决办法。一个简单的方法是使用偶数计数器来捕获两个回调中的一个。
import 'package:chat_notification/model/message.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
class MessageWidget extends StatefulWidget {
@override
_MessageWidgetState createState() => _MessageWidgetState();
}
class _MessageWidgetState extends State<MessageWidget> {
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
List<Message> messages = [];
static int i = 0;
@override
void initState() {
// TODO: implement initState
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> response) async {
if(i%2==0) {
print("onMessage: $response");
// something else you wanna execute
};
i++;
},
onLaunch: (Map<String, dynamic> response) async {
print("onLaunch: $response");
},
onResume: (Map<String, dynamic> response) async {
print("onResume: $response");
},
);
}
@override
Widget build(BuildContext context) => ListView(
children: messages.map(buildMessage).toList(),
);
Widget buildMessage(Message message) => ListTile(
title: Text(message.title),
subtitle: Text(message.body),
);
}这将只运行一次代码!可以类似地用于onLaunch和onResume。
发布于 2021-10-10 19:55:34
我用firebase_messaging来面对这个问题:^10.0.4
通过文献,我发现这是一个错误,不时发生在不同的版本。
知道了这一点,我个人不会使用答案中提到的方法(使用布尔标志、奇数/偶数标志)。
我用一个时间控制的信号量来解决这个问题。
static int semaphore = 0;
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (semaphore != 0) {
return;
}
semaphore = 1;
Future.delayed(Duration).then((_) => semaphore = 0);
handleMessage(message);
});此外,还避免了并行远程消息的启动(基于您传递的持续时间)--尽管我无法想象远程消息可能同时启动的现实情况。
发布于 2020-06-30 07:25:22
--这是解决此问题的完美代码。我们需要做的就是声明一个静态bool变量,它将与onResume()一起工作。在缺少一些逻辑之前,这就是为什么它要调用两次。现在,它正在为我工作。希望,你也会从这里得到帮助。。
onMessage: // ignore: missing_return (Map<String, dynamic> msg) { if(!isNotified) { print("onMessage called"); print(msg); DocumentReference documentReference = Firestore.instance.collection('PushNotifications').document(); documentReference.setData({ "payload": msg }); } isNotified = !isNotified; }

https://stackoverflow.com/questions/59692330
复制相似问题