java - 实现 Firebase (Flutter) 通知时,我收到错误

标签 java android flutter dart

首先,我为自己不是英语母语而深表歉意。

但是我遇到的问题如下。

1) 我正在按照教程(Youtube/文档)在 Flutter 中实现通知。

2)按照文档的指示,我进行了所有推荐的设置。它们如下:

  • 在 Console Flutter 中生成 google-services.json 文件

  • 将 apply plugin: 'com.google.gms.google-services' 添加到应用文件夹中的 build.gradle

  • 将 build.gradle 中的类路径“com.google.gms: google-services: 4.2.0”添加到 android 文件夹的根目录

  • 将 Intent 过滤器 (FLUTTER_NOTIFICATION_CLICK) 添加到 AndroidManifest.xml 文件

这是android文件夹的build.gradle。

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'        
        classpath 'com.google.gms:google-services:3.2.0'
    }
}

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
}

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.1.0"
            }
        }
    }
}

这是应用程序文件夹的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 27

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.tccversao2"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    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 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

apply plugin: 'com.google.gms.google-services'

这是我的代码

@immutable
class Message {
  final String title;
  final String body;

  const Message({
    @required this.title,
    @required this.body,
  });
}

class MessagingWidget extends StatefulWidget {
  @override
  _MessagingWidgetState createState() => _MessagingWidgetState();
}

class _MessagingWidgetState extends State<MessagingWidget> {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  final List<Message> messages = [];

  @override
  void initState() {
    super.initState();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        final notification = message['notification'];
        setState(() {
          messages.add(Message(
              title: notification['title'], body: notification['body']));
        });
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");

        final notification = message['data'];
        setState(() {
          messages.add(Message(
            title: '${notification['title']}',
            body: '${notification['body']}',
          ));
        });
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
  }

  @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: ^4.0.0+1 firebase_core:^0.3.0

我收到以下错误消息:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'com.android.support:support-fragment' has different version for the compile (26.1.0) and runtime (27.1.1) classpath. You should manually set the same version via DependencyResolution

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 6s
Gradle task assembleDebug failed with exit code 1

我已经尝试在我的 build.gradle (在 android 文件夹中)中使用类路径 'com.google.gms: google-services: 4.3.0',但在这种情况下,我收到另一个错误:

* Error running Gradle:
ProcessException: Process "E:\Flutter\projetos\tcc_versao2\tcc_versao2\android\gradlew.bat" exited abnormally:

FAILURE: Build failed with an exception.

* Where:
Build file 'E:\Flutter\projetos\tcc_versao2\tcc_versao2\android\app\build.gradle' line: 24

* What went wrong:
A problem occurred evaluating project ':app'.
> ASCII

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3s
  Command: E:\Flutter\projetos\tcc_versao2\tcc_versao2\android\gradlew.bat app:properties

Please review your Gradle project setup in the android/ folder.

*也许重要的是:我正在我的手机上调试(小米 Note 3 pro)

我完全迷失在这个错误中,如果有人能帮助我,我会非常高兴。

最佳答案

您应该查看您正在使用的库。

该错误表示它发现了 com.android.support:support-fragment 的两个不同版本。打开 android/build.gradle 并检查此处是否存在该依赖项并将其更新到版本 27.1.1。

如果您没有直接在应用程序中找到依赖项,它可能来自另一个过时的依赖项或 Flutter 插件。也在 pubspec.yaml 中验证所有插件依赖项的更新。

关于java - 实现 Firebase (Flutter) 通知时,我收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583213/

相关文章:

java - 如何使用 Jenkins CI 部署 Java EE 应用程序?

java - 对于 Double 列表重写 equals() 和 hashCode() 的问题

java - 如何打印我的 LinkedList 中的条目?

android - 如何使滑动键盘像应用程序一样

java - 计算并删除字符串列表中的重复项,但将重复项添加到键中(任何惯用的 java8 式方式?)

java - 如何从另一个 Activity 获取背景?

java - Parcelable - ArrayList<String> NullPointerException

flutter - flutter -弹出直到没有路线的屏幕

dart - 动态制作带有文本和按钮的卡片

flutter - 使用警报管理器启动应用程序的正确方法是什么?