【问题标题】:How to use onBackgroundMessage in flutter with firebase (FCM), The onBackgroundMessage is not getting triggered如何在与firebase(FCM)的颤动中使用onBackgroundMessage,onBackgroundMessage没有被触发
【发布时间】:2021-03-08 01:48:05
【问题描述】:

我想将传入的 firebase 消息存储在本地数据库中,并且它对 onMessage 、 onLaunch 和 onResume 工作正常,但是当我尝试调试时 onBackgroundMessage 根本没有被触发但是当我的应用程序处于后台时我能够在通知托盘中获取通知

推送通知

Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {
  if (message.containsKey('data')) {
    print({message});
    PushNotificationService().storeInDatabase(message);
    final notifications = await PushNotificationService().getNotifications();
    final reversedNotifications = notifications.reversed.toList();
    homeScreenBloc.activityController.add(reversedNotifications);
  }
}

class PushNotificationService {
  BuildContext context;

  final FirebaseMessaging _fcm = FirebaseMessaging();

  Future initialise() async {
// print('notifcations length = ${notifications.length}');
// context = ctx;
    final notifications = await getNotifications();
    if (notifications != null) {
      homeScreenBloc.activityController.add(notifications);
    }
    if (Platform.isIOS) {
      _fcm.requestNotificationPermissions(IosNotificationSettings());
    }

    _fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        print({message});
        storeInDatabase(message);
        final notifications = await getNotifications();
        final reversedNotifications = notifications.reversed.toList();
        homeScreenBloc.activityController.add(reversedNotifications);
      }

      /// called when the notification is clicked while the app is in running in foreground
      /// notification fields sends title and body but for other two cases its blank
      /// so need to send in data field

      ,
// called when the notification is clicked while the app is closed(not running)
      onLaunch: (Map<String, dynamic> message) async {
        print({message});
        storeInDatabase(message);
        final notifications = await getNotifications();
        final reversedNotifications = notifications.reversed.toList();
        homeScreenBloc.activityController.add(reversedNotifications);
      },
// called when the notification is clicked while the app is running in the background
      onResume: (Map<String, dynamic> message) async {
        print({message});
        storeInDatabase(message);
        final notifications = await getNotifications();
        final reversedNotifications = notifications.reversed.toList();
        homeScreenBloc.activityController.add(reversedNotifications);
      },
      // called while the app is running in the background
      onBackgroundMessage: Platform.isIOS ? null : backgroundMessageHandler,
      // }
    );
  }

AndroidManifest.xml

 <application android:usesCleartextTraffic="true" android:name=".Application" ....

Application.kt

package com.altorumleren.alfinityiot;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
  @Override
  public void onCreate() {
    super.onCreate();
    FlutterFirebaseMessagingService.setPluginRegistrant(this);
  }

  @Override
  public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
  }
}

build.gradle

dependencies {
    implementation 'com.google.firebase:firebase-messaging:<21.0.0>'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation "com.google.firebase:firebase-messaging:20.2.4"
}

我运行应用程序时的日志

Launching lib\main.dart on AOSP on IA Emulator in debug mode...
lib\main.dart:1
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 26): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 34): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 53): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 64): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 89): Expecting a top level declaration
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (9, 89): Function declaration must have a name
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (17, 51): Expecting an element
e: C:\Users\adity\Desktop\alfinity\android\app\src\main\kotlin\com\example\alfinity\Application.kt: (10, 3): This annotation is not applicable to target 'expression'

【问题讨论】:

  • 您找到解决方案了吗?

标签: android firebase flutter push-notification


【解决方案1】:

似乎您没有提及您的有效负载,但为了触发 onBackgroundMessege 您需要确保您的有效负载不包含通知。例如:

    const payload = {
        data: {
            title: 'title',
            body: 'body',
        }
    }

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2021-05-16
    • 2021-04-05
    • 2021-06-16
    • 2020-06-16
    • 2022-11-23
    • 2021-04-22
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多