【问题标题】:FCM Test Notifications Recivies By The Device But The Pubilc Ones Does Not设备收到FCM测试通知,但公开的没有
【发布时间】:2021-09-08 19:43:33
【问题描述】:

我在我的 Flutter 应用上使用 Firebase Cloud Message。当我使用设备令牌发送测试通知时,设备会收到通知;但是当我从 FCM 控制台推送通知时,它不起作用。

注意:“FirebaseMessaging.onBackgroundMessage”方法适用于测试通知并打印出消息标题。对于发布的通知,该方法不起作用。

ma​​in.dart:

    ...
  Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print("Background message");
  print('Handling a background message ${message.ttl}');
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  await Firebase.initializeApp();
  await loadImages();
  await GetStorage.init();
  NotificationService.firebaseMessageInit();
  runApp(
    MyApp(),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StatefulWrapper(
      onInit: () async {
        FirebaseMessaging.onMessage.listen((RemoteMessage event) {
          print("message recieved");
          print(event.notification!.body);
        });
        FirebaseMessaging.onMessageOpenedApp.listen((message) {
          print('Message clicked!');
        });
        FirebaseMessaging.instance
            .getToken()
            .then((value) => print(value.toString() + " token"));
     ...

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cekilis">
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
   <application
        android:label="cekilis"
       android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher"
       android:allowBackup="true">

       <meta-data
           android:name="com.google.android.gms.ads.APPLICATION_ID"
           android:value="..."/>
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            ...
            android:hardwareAccelerated="true"
            android:directBootAware="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />

            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate 
  GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

app/build.gradle

...
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
...
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:28.1.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.android.gms:play-services-ads:20.2.0'
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    implementation 'com.google.firebase:firebase-messaging-ktx:22.0.0'
    implementation 'com.google.firebase:firebase-messaging-directboot:22.0.0'
}

project/build.gradle

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

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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

颤振医生

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.2, on Microsoft Windows [Version 10.0.19041.1052], 
locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] VS Code (version 1.57.1)
[√] Connected device (3 available)

• No issues found!

*Dart Version  2.13.3*

【问题讨论】:

  • 您如何将通知从 firebase 控制台发送到主题?或以编程方式到特定的 FCM 令牌?
  • 我尝试了向主题发送通知的云功能。我在日志上看到“函数执行耗时 303 毫秒,以状态完成:'ok'”,但通知不起作用。因此,我尝试通过编写标题和正文来简单地从 firebase 控制台发布通知。它也没有工作。
  • 在您在第 5 步中发送通知的云功能控制台中附加选项(可选)您是否添加了自定义数据 click_action: FLUTTER_NOTIFICATION_CLICK ?

标签: android ios flutter dart firebase-cloud-messaging


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 2018-11-14
    • 2023-01-11
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多