0

This the data that is being sent to the app

    $fields = array (  
        'to' => $id,
        'notification' => array (   
            "title" =>  $title,
            "body" =>  $message,
        )
        ,      
        'data' => array (
            "title" =>  $title,
            "message" =>  $message,
            "type" => $type,
            "responseId" => $responseId,
            "prompterType" => $prompterType
        )
    );

For example:-

Array ( [type] => Grouping [responseId] => 250 [message] => Array ( [daodao0912@gmail.com] => Array ( [name] => mai mai [matches] => tai, dao [table] => 1 ) ) [prompterType] => -1 )
string(143) "{"multicast_id":3893029672353112501,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1662052156406640%014b8671014b8671"}]}" 

To process this I have done the following. Below is the code for my main.dart. I have added a backgroundhandler as well as an onMessage.listen but still it is not doing the required thing. What more changes should I make?

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  print('Handling a background message ${message.messageId}');
  Map<String,dynamic> data = message.data;
  processdata(data);
  getDataRound1();
  getDataRound2();
}

Future<void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: const FirebaseOptions(
    apiKey: 'AIzaSyCGPCpK9XxKgxTzfnXOSA-9HAeIWtQ0Zhk',
    appId: '1:22990997901:android:60a134f76b798158c995c4',
    messagingSenderId: '22990997901',
    projectId: 'activepeers-ai',
    storageBucket: 'activepeers-ai.appspot.com',
  )
  );
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);


  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    print('Got a message whilst in the foreground!');
    print('Message data: ${message.data}');

    if (message.notification != null) {
      print('Message also contained a notification: ${message.notification}');
    }
  });

  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    importance: Importance.max,
  );

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  FlutterLocalNotificationsPlugin();

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    RemoteNotification? notification = message.notification;
    AndroidNotification? android = message.notification?.android;
    print('${message.messageId}');
    // If `onMessage` is triggered with a notification, construct our own
    // local notification to show to users using the created channel.
    if (notification != null && android != null) {
      flutterLocalNotificationsPlugin.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              channel.id,
              channel.name,
              icon: android.smallIcon,
              // other properties...
            ),
          ));
    }
  });
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: SplashScreen(),
    );
  }
}

Below is the function that processes the data

String type = '';
String responseId = '';
int prompterType = 0;
String name ='';
String matching = '';
int table = 0;
   void processdata(Map<String,dynamic> data) async{
    type = data["type"];
    responseId = data["responseId"];
    prompterType = data["prompterType"];
   Map<String,dynamic>personalInfo = data["message"][0];
    name = personalInfo["name"];
    matching = personalInfo["matches"];
    table = personalInfo["table"];
   }

This is not working and I have not been able to figure out why. Any help would be appreciated.

1 Answers1

0

onMessage.listen only works when your app is running

the background message handler is for background push notifications