Dev/Flutter

[Flutter] FirebaseMessaging background 수신 이슈 / Flutter3.3 이상

healthyryu 2023. 7. 3. 19:02
[ERROR:flutter/lib/ui/dart_runtime_hooks.cc(38)] Dart Error: Unhandled exception:
        NoSuchMethodError: No top-level getter 'backgroundHandler' declared.
        Receiver: top-level
        Tried calling: backgroundHandler
        #0      NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:191)
        #1      FfiTrampoline___getCallbackFromHandle$Method$FfiNative$Ptr (dart:ffi)
        #2      _getCallbackFromHandle (dart:ui/natives.dart:118)
        #3      PluginUtilities.getCallbackFromHandle.<anonymous closure> (dart:ui/plugins.dart:74)
        #4      _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:535)
        #5      PluginUtilities.getCallbackFromHandle (dart:ui/plugins.dart:73)
        #6      _firebaseMessagingCallbackDispatcher.<anonymous closure> (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:40)
        #7      MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:547)
        #8      MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:540)
        #9      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:479)
        #10     _invoke2 (dart:ui/hooks.dart:174)
        #11     _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:40)
        #12     _Channel.push (dart:ui/channel_buffers.dart:130)
        #13     ChannelBuffers.push (dart:ui/channel_buffers.dart:326)
        #14     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:686)
        #15     _dispatchPlatformMessage (dart:ui/hooks.dart:86)

 

현재 Firebase 관련 라이브러리 버전

firebase_core_platform_interface: 4.8.0
firebase_core: ^2.13.0
firebase_crashlytics: ^3.3.1
firebase_messaging: ^14.6.1
firebase_dynamic_links: ^5.3.1

 

이슈

FirebaseMessaging 에서 백그라운드(Background)로 들어오는 FCM 을 수신하기 위한 설정을 했었다. 그런데 백그라운드 상태에서 위와같은 에러 로그가 발생했다.

 

처음에는 백그라운드를 처리하는 함수가 설정되어있는데 없다고 나왔기에 Proguard 문제라고 추측을 했다. 그래서 구글링을 하기전에 Proguard 에 대한 처리를 먼저 진행했으나 똑같은 현상이 발생했다. 그래서 구글링을 해보니 백그라운드 함수에 Annotation 처리를 해야함을 알게 되었다.

 

해당 이슈 해결 링크

 

Main 코드

main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  ...
}

 

backgroundHandler (before)

Future<void> backgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  FirebaseMessageSetup().receiveMessage(message);
}

 

backgroundHandler (Solution)

@pragma('vm:entry-point')
Future<void> backgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  FirebaseMessageSetup().receiveMessage(message);
}
반응형