firebase 如何从本地导航本地通知从后台?

n53p2ov0  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(185)

我在我的应用程序中使用flutter本地通知。当应用程序在后台时,我无法通过单击本地通知导航到特定页面。

它给了我错误:

_AssertionError ('package:flutter_local_notifications/src/platform_flutter_local_notifications.dart': Failed assertion: line 1018 pos 12: 'callback != null':           The backgroundHandler needs to be either a static function or a top 
      level function to be accessible as a Flutter entry point.)

这是我的代码:

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();


     Future<void> listiningToNotification() async {
  FirebaseMessaging.onMessageOpenedApp
      .listen((message) => notificationOnClick(message));
}

Future<void> localNotification(RemoteMessage message) async {
  AndroidNotificationChannel channel = AndroidNotificationChannel(
    "local_notification",
    message.data["booking_id"],
    description: message.data["task_id"],
    importance: Importance.max,
    playSound: true,
  );
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);
  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings("@drawable/notification_icon");
  const InitializationSettings initializationSettings =
      InitializationSettings(android: initializationSettingsAndroid);
  flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
    onDidReceiveNotificationResponse: (details) => notificationOnClick(message),
    onDidReceiveBackgroundNotificationResponse: (details) =>
        firebaseMessagingBackgroundHandler,
  );
  RemoteNotification? notification = message.notification;
  AndroidNotification? android = message.notification?.android;
  if (notification != null && android != null) {
    flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(channel.id, channel.name,
              channelDescription: channel.description,
              importance: channel.importance,
              playSound: channel.playSound,
              color: Colors.white),
        ),
        payload: notification.body);
  }
}

@pragma('vm:entry-point')
Future<void> localNotificationBackground(
    {required NotificationResponse details}) async {
  print(details.payload);
}

我做错了什么,还是我错过了什么?
我的firebase后台通知也有这个功能,是这个原因吗?

@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  List<FirebaseApp> app = Firebase.apps;
  if (app.length < 2) {
    await Firebase.initializeApp(
        name: "xxxxxxxxxx", options: DefaultFirebaseOptions.currentPlatform);
  }
  FirebaseMessaging messaging = FirebaseMessaging.instance;
  await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: true,
    criticalAlert: false,
    provisional: true,
    sound: true,
  );
}
falq053o

falq053o1#

在main函数中,只需替换:

onDidReceiveBackgroundNotificationResponse: (details) =>
        firebaseMessagingBackgroundHandler,

有:

onDidReceiveBackgroundNotificationResponse: firebaseMessagingBackgroundHandler,

这样就可以直接调用顶级函数。
并将处理程序的定义从:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {

收件人:

void firebaseMessagingBackgroundHandler(
  final NotificationResponse details,
) {

希望这对某人有帮助。

相关问题