Making flutter app to detect incoming server.js request and ring/vibrate the device

I have wrote the code for server.js and main.dart.

Functionality which I am trying to achieve is very simple but somehow I cannot make it work.

server receives an incoming request, app fetches that request from server and ring/vibrate the device.

**main.dart code
**

void main() {
  runApp(MyApp());
}

String receiverDeviceId =
    ''; // Define and declare the receiverDeviceId variable

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      themeMode: ThemeMode.dark,
      home: MyHomePage(),
    );
  }
}

Future<String> fetchReceiverDeviceId(String serverUrl) async {
    final response =
        await http.get(Uri.parse('$serverUrl/getReceiverDeviceId'));

    if (response.statusCode == 200) {
      final result = jsonDecode(response.body);
      return result['receiverDeviceId'];
    } else {
      throw Exception(
          'Failed to fetch receiverDeviceId from the server. Status code: ${response.statusCode}');
    }
  }

void sendSlap(String serverUrl, String senderDeviceId,
      Function(String) onServerResponse) async {
    try {
      String receiverDeviceId = await fetchReceiverDeviceId(serverUrl);

      final response = await http.post(
        Uri.parse('$serverUrl/notifySlap'),
        headers: {'Content-Type': 'application/json'},
        body: jsonEncode(
            {'deviceId': senderDeviceId, 'receiverDeviceId': receiverDeviceId}),
      );

      if (response.statusCode == 200) {
        final result = jsonDecode(response.body);

        if (result['success']) {
          print(
              'Slap notification sent from $senderDeviceId to $receiverDeviceId');
          showSnackbar(
              'Slap notification sent from $senderDeviceId to $receiverDeviceId');

          // Notify the app that the server received the slap request
          onServerResponse('Server received slap notification');
        } else {
          print('Error sending slap notification: ${result['message']}');
          showSnackbar('Error sending slap notification: ${result['message']}');
        }
      } else {
        print('Slap notification request failed. Please try again.');
        showSnackbar('Slap notification request failed. Please try again.');
      }
    } catch (e) {
      print('Error sending slap notification: $e');
      showSnackbar('Error sending slap notification. Please try again.');
    }
  }

  void receiveSlap(String senderDeviceId, String receiverDeviceId) {
    // Logic to handle receiving a slap notification
    print(
        'Slap notification received from $senderDeviceId on $receiverDeviceId');
    showSnackbar(
        'Slap notification received from $senderDeviceId on $receiverDeviceId');

    // Vibrate the device
    vibrateDevice();

    // Play the notification sound
    playNotificationSound();
  }

  void playNotificationSound() async {
    AudioCache player = AudioCache();
    await player.load('noti/notification_sound.mp3');
  }

  void vibrateDevice() {
    Vibration.vibrate(duration: 500); // Vibrate the device for 500 milliseconds
  }

ElevatedButton(
                    onPressed: () async {
                      print('Slap button pressed'); // Add this debug statement
                      final response = await http
                          .get(Uri.parse('$serverUrl/getReceiverDeviceId'));
                      if (response.statusCode == 200) {
                        receiverDeviceId =
                            jsonDecode(response.body)['receiverDeviceId'];
                        print(
                            'Receiver Device ID: $receiverDeviceId'); // Add this debug statement
                        sendSlap(serverUrl, deviceId!, (String serverResponse) {
                          print(
                              serverResponse); // This will notify the app that the server has received the slap request
                        });
                      } else {
                        print(
                            'Failed to fetch receiverDeviceId from the server. Status code: ${response.statusCode}'); // Add this debug statement
                      }
                    },
                    child: Text('Slap!'),
                  ),

**server.js code
**

fastify.post('/notifySlap', (request, reply) => {
  const deviceId = request.body.deviceId;
  const receiverDeviceId = request.body.receiverDeviceId;

  const connectedDevicesToNotify = connectedDevices.filter(id => id !== deviceId);

  if (connectedDevicesToNotify.length > 0) {
    console.log(`Slap notification sent to devices: ${connectedDevicesToNotify.join(', ')}`);
    sendNotificationCommandToDevices(connectedDevicesToNotify); // Send command to devices to play sound or vibrate
  }

  if (deviceId === receiverDeviceId) {
    console.log(`Slap notification sent to device: ${deviceId}`);
    // Add logic here to handle the slap received by the same device
    // For example, you can play a sound or vibrate the device
  }

  reply.send({ success: true });
});

fastify.get('/getReceiverDeviceId', (request, reply) => {
  const receiverDeviceId = generateReceiverDeviceId();
  reply.send({ receiverDeviceId });
});

function sendNotificationCommandToDevices(devices) {
  devices.forEach(deviceId => {
    if (connectedDevices.includes(deviceId)) {
      console.log(`Sending slap notification to device: ${deviceId}`);
      if (isVibratingDevice(deviceId)) {
        console.log(`Vibrating device: ${deviceId}`);
        // Add code here to trigger device vibration
      } else {
        console.log(`Sending sound notification to device: ${deviceId}`);
        // Add code here to play a sound notification
      }
    } else {
      console.log(`Device ${deviceId} is not connected.`);
    }
  });
}

function isVibratingDevice(deviceId) {
  // Add logic here to determine if the device should vibrate upon receiving a slap notification
  // For example, you can check a database or a list of vibrating devices
  return true; // Return true if the device should vibrate, false otherwise
}

// Add a new endpoint to fetch server logs
fastify.get('/serverLogs', (request, reply) => {
  // Implement logic to fetch and return server logs here
  // You can return an array of log messages from the server
  const serverLogs = ['I/flutter (3471): Server received slap notification', 'Vibrating device: '];
  reply.send(serverLogs);
});

I have tried to fetch the server log and make the app ring/vibrate by matching the server logs, but nothing happens.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật