【问题标题】:Flutter Firebase Cloud Messaging onMessage is triggered twiceFlutter Firebase Cloud Messaging onMessage 被触发两次
【发布时间】:2020-04-28 17:34:51
【问题描述】:

我已经实现了 firebase_messaging flutter 包建议的基本配置。但是,每次我在我的颤振应用程序上收到通知时,都会触发两次。我正在使用 firebase_messaging 6.0.9、Dart 2.7.0 和 Flutter 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 中不再存在。我发现的最佳解决方案是更新软件包。重复的消息不再存在。

【问题讨论】:

  • 我有同样的问题,如果不是秘密,你的应用在 firebase 项目中的名称是什么?
  • 如果你的意思是包的名字就是com.example.chat_notification
  • Bingo,我还有一个项目名称中带有下划线。你可以尝试重命名它吗?
  • 是的,我尝试过,但仍然是同样的问题,也许我会尝试更具体的名称。
  • 我同样的错误:(

标签: firebase flutter firebase-cloud-messaging


【解决方案1】:

更新:

这在最新版本的 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。

【讨论】:

  • 同感的小改进:static bool _isDoubleMessage = false; onMessage: (Map message) async { if(!_isDoubleMessage) { print('on message $message'); //setState(() => _message = message["notification"]["title"]); } _isDoubleMessage = !_isDoubleMessage; },
  • 我使用的是 10.0.2 版,但由于某种原因,问题仍然存在
【解决方案2】:

我在使用 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);
});

还避免并发远程消息启动(基于您传递的持续时间) - 尽管我无法想象可能同时启动远程消息的现实世界情况。

【讨论】:

    【解决方案3】:

    这是解决这个问题的完美代码。 我们只需要声明一个静态布尔变量,它将与 onResume() 一起使用。 在缺少一些逻辑之前,这就是它调用两次的原因。 现在,它对我有用。希望您也能从中得到帮助。

    onMessage: // ignore: missing_return (Map&lt;String, dynamic&gt; msg) { if(!isNotified) { print("onMessage called"); print(msg); DocumentReference documentReference = Firestore.instance.collection('PushNotifications').document(); documentReference.setData({ "payload": msg }); } isNotified = !isNotified; }

    Github Link

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,原因是我最近重命名了我的项目。我注意到(太久之后)其中一个通知横幅上写着旧的应用名称,而另一个上写着新的应用名称。卸载并重新安装应用程序(以及flutter clean)后,双重通知消失了。

      简而言之,如果其他方法都不起作用,请尝试再次卸载并重新安装您的应用。

      【讨论】:

        【解决方案5】:

        更新到最新的 FireFlutter 包解决了这个问题:

        为了非空安全:

        firebase_messaging: ^8.0.0-dev.15
        firebase_core: ^0.7.0
        

        【讨论】:

          猜你喜欢
          • 2017-04-02
          • 2018-01-08
          • 1970-01-01
          • 2017-08-25
          • 2018-05-16
          • 1970-01-01
          • 2018-10-18
          • 2019-08-18
          • 2022-10-01
          相关资源
          最近更新 更多