Community Member
ย ยทย Developer

Notification handler stopped working after the new update.

I have written this code:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/actions/actions.dart' as action_blocks;
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:intl/intl.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
import 'package:go_router/go_router.dart';

bool isPermissionRequestInProgress = false;

Future handlePushNotification(
  //BuildContext context,
  Future<dynamic> Function()? callback,
) async {
  if (isPermissionRequestInProgress) {
    print('Permission request is already in progress.');
    return;
  }

  //DateTime now = DateTime.now();

  NotificationSettings settings =
      await FirebaseMessaging.instance.getNotificationSettings();

  if (settings.authorizationStatus == AuthorizationStatus.notDetermined) {
    isPermissionRequestInProgress = true;

    await FirebaseMessaging.instance.requestPermission().then((_) {
      isPermissionRequestInProgress = false;
    }).catchError((e) {
      isPermissionRequestInProgress = false;
      print(e);
    });
  }

  //FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    // // Called when the app is in the foreground and a push notification is received.
    // // Here, you can show a notification dialog or a custom UI to display the message.
    // // message.data is a map that stores your message data
    // print('Got a message whilst in the foreground!');
    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    // print(DateFormat('yyyy-MM-dd HH:mm:ss').format(now));
    print(message.data);
    print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    //action_blocks.notificationHandler(context);
    executeCallback(message, callback);
  });

  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    print("=================================================================");
    // print(DateFormat('yyyy-MM-dd HH:mm:ss').format(now));
    print(message.data);
    print("==================================================================");
    //action_blocks.notificationHandler(context);
    executeCallback(message, callback);

    //try {
    //  FlutterAppBadger.removeBadge();
    //  } catch (e) {
    //   print(e);
    // }
    //  GoRouter.of(context).go('/splashScreen');
  });
}

Future<String> executeCallback(
    RemoteMessage message, Future<dynamic> Function()? callback) async {
  print(message.data);

  FFAppState().notificationData = await message.data; // Store the message data
  if (callback != null) {
    await callback(); // Execute the callback if it's not null
    //await action_blocks.notificationHandler(context);
  }
  return '';
}

// Future<void> backgroundMessageHandler(RemoteMessage message) async {
//   print("Handling a background/terminated message: ${message.messageId}");
//   // You can call some services or save the data locally.
//   // However, UI-related tasks are not permitted here.

//   executeCallback(message, null); // Call your executeCallback function
// }

I run this custom action at the start of the page. I pass a callback action:

The callback action is to show a custom pop up:

This code was working perfectly fine until the latest update. Now I get this error:

Any ideas?

2
9 replies