在后台关闭应用程序时未收到 FCM 推送通知 - Flutter android

时间:2021-03-12 01:20:35

标签: android firebase flutter push-notification firebase-cloud-messaging

我遇到了 Firebase Cloud Messaging(FCM) 的问题。我的问题是:在最近的选项卡中关闭应用程序时,设备未收到 FCM 推送通知。 当应用程序在前台或后台时,它工作正常。但是,当我从后台关闭它时,我没有在通知托盘中收到任何通知。

我看了很多帖子并尝试了很多解决方案,但在我的情况下没有任何效果:(

我尝试过的步骤:

  1. 我在依赖项中添加了 fcm 并在 build.gradle 中应用了 google 插件 这是我的app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.myapplication.id"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        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 {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:26.5.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation "androidx.browser:browser:1.3.0"
    implementation 'com.google.firebase:firebase-messaging:21.0.1'
}

2.我在我的 project/build.gradle 中添加了 google-services 依赖 这是我的project/build.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath 'com.google.gms:google-services:4.3.5'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}
  1. 在 manifest.xml 中设置 android:enabled="true" 和 android:exported="true" 这是我的app/src/main/Manifest.xml
<application
        android:label="partner_location_project"
        android:icon="@mipmap/ic_launcher">

        <activity
            android:exported="true"
            android:enabled="true"
            android:name=".MainActivity"
   ....
   ....
   ....
<intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

这是我声明 FCM 的 dart 文件。

class _CheckAuthState extends State<CheckAuth> {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  void fcm_listener() async {
    try {
      var token = await _firebaseMessaging.getToken();
      print("fcm token = $token");
      _firebaseMessaging.configure(
        onLaunch: (Map<String, dynamic> message) async {
          print("onLaunch $message");
        },
        onMessage: (Map<String, dynamic> message) async {
          print("onMessage $message");
        },
        onResume: (Map<String, dynamic> message) async {
          print("onResume $message");
        },
      );
    } catch (e) {
      print("fcm exception");
      print(e.toString());
    }
  }
void initState() {
    // TODO: implement initState
    super.initState();
    fcm_listener();
  }

感谢任何想法!

0 个答案:

没有答案