registerPrivateNotificationHandler method

void registerPrivateNotificationHandler()

Register handler for private mailbox direct streams

Implementation

void registerPrivateNotificationHandler() {
  host.setStreamHandler(mailboxNotifyProtocolId, (stream, remotePeer) async {
    try {
      final data = await StreamUtils.readLengthPrefixedFrame(stream);
      final json = jsonDecode(utf8.decode(data));
      final notification = MailboxNotification.fromJson(json);

      // CRITICAL: Include the server peer ID so retrieval uses the same server
      // that sent the notification (and has the message)
      // A private mailbox is always the recipient's own.
      final notificationWithServer = notification
          .withServerPeerId(remotePeer.toBase58())
          .withOwnerIfMissing(host.id.toBase58());

      _logger.info('📬 Private notification from ${remotePeer.toBase58().substring(0, 12)}...: ${notification.mailboxPath}');
      _notificationController.add(notificationWithServer);

      await stream.close();
    } catch (e) {
      _logger.warning('Error handling private notification: $e');
    }
  });

  _logger.info('📡 Registered private notification handler');
}