我已经实现了firebase_messaging flutter软件包建议的基本配置。但是,每次我在flutter应用程序onMessage上收到通知时,都会触发两次。我正在使用firebase_messaging 6.0.9,Dart 2.7.0和Flutter 1.12.13 + hotfix.5。
这是我的[project] /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
}
这是我的[project] /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中不再提供此功能。我发现最好的解决方案是更新程序包。重复的消息不再存在。
答案 0 :(得分:0)
这是此问题的理想代码。 我们只需要声明一个静态布尔变量,它将与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; }
答案 1 :(得分:0)
我遇到了同样的问题,原因是最近我将项目重命名。我注意到(过长的时间),其中一个通知标语中说的是旧应用程序名称,另一个是新的应用程序名称。卸载并重新安装应用程序(以及flutter clean
)后,双重通知消失了。
简而言之,如果没有其他效果,请尝试再次卸载并重新安装您的应用。
答案 2 :(得分:0)
更新到最新的 FireFlutter 包解决了这个问题:
对于非空安全:
firebase_messaging: ^8.0.0-dev.15
firebase_core: ^0.7.0