【问题标题】:FCM push notification not received by device when app is closed in background - Flutter Android当应用程序在后台关闭时设备未收到 FCM 推送通知 - Flutter Android
【发布时间】:2021-06-10 08:41:44
【问题描述】:

我遇到了 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.我在我的项目/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();
  }

我正在真实设备(realme 6 pro)上对其进行测试。 (我正在使用 firebase_messaging:^9.0.0) 任何想法表示赞赏!

【问题讨论】:

  • 您是否尝试过清理和重建?
  • 您找到解决方案了吗?我目前面临完全相同的问题,但仅在 Realme 设备上。在其他测试设备上收到推送。也许是 realme 的一些电池优化?
  • @BenjaminMenrad 我不知道它是如何解决的。经过一番清理、重建和重新启动后,它会自动解决

标签: android firebase flutter gradle firebase-cloud-messaging


【解决方案1】:

检查这个工作示例:Flutter_Firebase_Notification

示例代码:

Future<void> myBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  return MyAppState()._showNotification(message);
}

Future<void> main() async {
  await WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(myBackgroundHandler);
  runApp(MaterialApp(home: MyApp()));
}

Full Code

【讨论】:

    猜你喜欢
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2016-10-17
    • 2021-06-13
    相关资源
    最近更新 更多