B Bubbl Docs

Flutter SDK Usage Examples

Boot + permission flow

import 'package:bubbl_flutter_sdk/bubbl_flutter_sdk.dart';
import 'package:geolocator/geolocator.dart';

final BubblFlutterSdk sdk = BubblFlutterSdk.instance;

Future<void> startBubbl(String apiKey) async {
  await sdk.boot(
    apiKey: apiKey,
    options: const BubblBootOptions(
      environment: BubblEnvironment.staging,
      segmentationTags: <String>['beta_user'],
      geoPollIntervalMs: 300000,
      defaultDistance: 25,
    ),
  );

  if (!await sdk.notificationGranted()) {
    await sdk.requestPushPermission();
  }

  // Request location using your location-permission flow.
  await Geolocator.requestPermission();

  await sdk.startLocationTracking();
}

Refresh geofences and campaigns

Future<void> refreshAt(double latitude, double longitude) async {
  await sdk.refreshGeofence(latitude: latitude, longitude: longitude);
  await sdk.forceRefreshCampaigns();

  final bool hasCampaigns = await sdk.hasCampaigns();
  final int count = await sdk.getCampaignCount();
  print('campaigns: $hasCampaigns ($count)');
}

Push segments and correlation id

await sdk.updateSegments(<String>['vip', 'early_access']);

await sdk.setCorrelationId('user-12345');
final String correlationId = await sdk.getCorrelationId();
print('correlationId: $correlationId');

Configuration and privacy text

final config = await sdk.getCurrentConfiguration();
final cachedPrivacyText = await sdk.getPrivacyText();
final refreshedPrivacyText = await sdk.refreshPrivacyText();

print(config?.notificationsCount);
print(cachedPrivacyText);
print(refreshedPrivacyText);

Notification stream + CTA handling

final sub = sdk.notificationEvents().listen((payload) async {
  final int? notificationId = int.tryParse('${payload['id'] ?? ''}');
  final String? locationId = payload['locationId']?.toString();

  if (notificationId != null && locationId != null && locationId.isNotEmpty) {
    await sdk.trackSurveyEvent(
      notificationId: '$notificationId',
      locationId: locationId,
      activity: 'notification_delivered',
    );

    await sdk.cta(notificationId: notificationId, locationId: locationId);
  }
});

Device logs

final info = await sdk.getDeviceLogStreamInfo();
print('device log info: $info');

await sdk.startDeviceLogStream(
  options: const BubblDeviceLogStreamOptions(intervalMs: 2500, maxLines: 80),
);

final logSub = sdk.deviceLogEvents().listen((snapshot) {
  print(snapshot['lines']);
});

// cleanup
await sdk.stopDeviceLogStream();
await logSub.cancel();

Tenant switching in test builds

await sdk.setTenantConfig(
  apiKey: 'NEW_API_KEY',
  environment: BubblEnvironment.staging,
);

await sdk.clearStoredConfig();
await sdk.boot(
  apiKey: 'NEW_API_KEY',
  options: const BubblBootOptions(environment: BubblEnvironment.staging),
);