반응형
Float - FCM 온백그라운드 메시지 핸들러가 등록되지 않아 Dart에서 백그라운드 메시지를 처리할 수 없습니다.
Fluter에서 FCM의 백그라운드 메시지를 처리하고 싶었지만, 백그라운드 모드에서 FCM이 앱에 메시지를 보내면서 로그에 오류가 발생했습니다.
pubspec.dll:
firebase_core: ^0.7.0
firebase_messaging: ^8.0.0-dev.14
기본 활동.kt :
package com.mydomain.myproject
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
Application.kt:
package com.mydomain.myproject
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundExecutor
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin
import io.flutter.plugins.pathprovider.PathProviderPlugin
class MyApplication : FlutterApplication(), PluginRegistrantCallback {
override
fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this)
FlutterFirebaseMessagingBackgroundExecutor.setPluginRegistrant(this)
}
override
fun registerWith(registry: PluginRegistry) {
PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin"))
FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"))
FlutterFirebaseMessagingPlugin.registerWith(registry.registrarFor("plugins.flutter.io/firebase_messaging"))
}
}
fcm 사용 코드 : 방금 메인 앱 클래스의 initState()에 있는 _initFcm()을 호출했습니다.제 목적은 앱이 백그라운드에 있을 때 백그라운드 FCM 메서드가 트리거되어야 하며 앱이 열리면 메시지 데이터를 환경설정에 저장하여 해당 작업을 수행해야 합니다.
void _initFcm() {
Firebase.initializeApp();
FirebaseMessaging.onMessageOpenedApp.listen((message) {
Logger.log("_messaging onMessageOpenedApp: ${message}");
});
FirebaseMessaging.instance.getInitialMessage().then((value) {
Logger.log("_messaging getInitialMessage: ${value}");
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
Map<String, dynamic> data = message.data;
Logger.log("_messaging onMessage: ${message}");
Fcm.showNotification(notification.body, notification.title);
String type = data['type'];
if (type == "view") {
String notifPath = data['subject'];
notificationProvider.addPath(notifPath);
Logger.log('new notification added to notificationList: ${notifPath}');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
Logger.log("_messaging onMessageOpenedApp: $message");
});
FirebaseMessaging.onBackgroundMessage((message) {
Logger.log("_messaging onBackgroundMessage: $message");
return;
});
}
로그 실행:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'toRawHandle' was called on null.
E/flutter ( 5030): Receiver: null
E/flutter ( 5030): Tried calling: toRawHandle()
E/flutter ( 5030): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter ( 5030): #1 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:181:42)
E/flutter ( 5030): #2 FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:107:14)
E/flutter ( 5030): #3 FirebaseMessaging.onBackgroundMessage (package:firebase_messaging/src/messaging.dart:103:31)
E/flutter ( 5030): #4 _ChatrAppState._initFcm (package:parsian_chatr/app/ui/chatr_app.dart:90:23)
E/flutter ( 5030): #5 _ChatrAppState.initState (package:parsian_chatr/app/ui/chatr_app.dart:69:5)
E/flutter ( 5030): #6 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
E/flutter ( 5030): #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter ( 5030): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter ( 5030): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter ( 5030): #10 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1252:16)
E/flutter ( 5030): #11 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1223:5)
E/flutter ( 5030): #12 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1165:17)
E/flutter ( 5030): #13 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2683:19)
E/flutter ( 5030): #14 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1164:13)
E/flutter ( 5030): #15 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:974:7)
E/flutter ( 5030): #16 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:955:7)
E/flutter ( 5030): #17 _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 5030): #18 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 5030): #19 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 5030): #20 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 5030): #21 _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 5030): #22 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 5030): #23 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter ( 5030): #24 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 5030): #25 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter ( 5030): #26 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter ( 5030): #27 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter ( 5030):
사용해 보세요.
MainApp 클래스가 포함된 파일에 핸들러를 추가합니다.클래스 밖에 추가하는 것을 기억하세요.
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message");
}
및 대체
FirebaseMessaging.onBackgroundMessage((message) {
Logger.log("_messaging onBackgroundMessage: $message");
return;
});
와 함께
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
참조: https://firebase.flutter.dev/docs/messaging/usage/#background-messages
백그라운드 메시지 처리기에는 다음과 같은 몇 가지 사항이 있습니다.
익명 함수가 아니어야 합니다.이 함수는 최상위 함수여야 합니다(예: 초기화가 필요한 클래스 메서드가 아님).
mzafer 답변 외에도,
파일 위에 아래의 함수 정의를 추가합니다.[ 존재하는 경우 클래스 외부 ]
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message");
}
이것은 저에게 효과가 있었습니다.
FirebaseMessaging.onMessageOpenedApp.listen((message) {
Logger.log("_messaging onMessageOpenedApp: ${message}");
});
코드에서 두 번 호출되었습니다.
언급URL : https://stackoverflow.com/questions/65864972/flutter-fcm-a-background-message-could-not-be-handled-in-dart-as-no-onbackgroun
반응형
'programing' 카테고리의 다른 글
C/C++에서 주조된 유형은 정확히 무엇입니까? (0) | 2023.07.29 |
---|---|
jQuery AJAX를 사용한 PHP의 여러 반환 값 (0) | 2023.07.29 |
SQL Server의 임시 테이블로 인해 '이름이 지정된 개체가 이미 있습니다' 오류가 발생했습니다. (0) | 2023.07.09 |
인라인 변수가 있는 다중 줄 파이썬 문자열을 만들려면 어떻게 해야 합니까? (0) | 2023.07.09 |
Python/SciPy를 위한 피크 찾기 알고리즘 (0) | 2023.07.09 |