sendNotification method

  1. @override
Future<NotificationResult> sendNotification({
  1. required NotificationData data,
  2. required NotificationTarget target,
})
override

Send a notification to specific targets

data - The notification content and metadata target - Who should receive the notification (users, tokens, topics)

Returns a NotificationResult indicating success/failure

Implementation

@override
Future<NotificationResult> sendNotification({
  required NotificationData data,
  required NotificationTarget target,
}) async {
  try {
    if (!_isFirebaseMessagingAvailable()) {
      return NotificationResult.failure(
        error: 'FCM not available. Add firebase_messaging dependency.',
      );
    }

    // For server-side sending, we would use HTTP API
    if (_config.serverKey != null) {
      return await _sendViaServerAPI(data, target);
    } else {
      // For client-side, we can only send to topics
      if (target.topic != null) {
        return await _sendToTopicClientSide(data, target.topic!);
      } else {
        return NotificationResult.failure(
          error: 'Server key required for sending to specific users/tokens',
        );
      }
    }
  } catch (e) {
    return NotificationResult.failure(error: e.toString());
  }
}