首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤振火基云消息传递onMessage被触发两次

颤振火基云消息传递onMessage被触发两次
EN

Stack Overflow用户
提问于 2020-01-11 06:55:04
回答 6查看 7.9K关注 0票数 10

我已经实现了firebase_messaging颤振包建议的基本配置。但是,每次在我的颤振应用程序onMessage上收到通知时,都会触发两次。我使用的是firebase_messaging 6.0.9、Dart 2.7.0和颤振1.12.13+hotfix.5。

这是我的项目/android/build.gradle

代码语言:javascript
运行
复制
    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

代码语言:javascript
运行
复制
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的代码

代码语言:javascript
运行
复制
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中不再存在。我找到的最佳解决方案是更新包。复制的消息不再存在。

EN

Stack Overflow用户

发布于 2020-08-04 16:02:09

我也有过同样的问题,造成这个问题的原因是我最近重新命名了我的项目。我注意到(过了太久),一个通知横幅上写着旧的应用程序名,另一个写着新的应用程序名。在卸载并重新安装应用程序(连同flutter clean)之后,双重通知就消失了。

总之,如果没有其他的工作,尝试卸载和重新安装您的应用程序。

票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59692330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档