initialize method
Future<bool>
initialize(
- SuperFCMConfig config, {
- NotificationCallback? onOpened,
- NotificationCallback? onForeground,
- NotificationCallback? onBackground,
Initializes the SuperFCM SDK with the provided configuration.
This method must be called before using any other SuperFCM functionality. It performs the following setup:
- Configures logging
- Initializes Firebase Messaging
- Sets up notification handlers
- Loads or creates device subscription
- Processes any pending operations
Parameters:
config
: Configuration settings for SuperFCMonOpened
: Optional callback for notifications that open the apponForeground
: Optional callback for foreground notificationsonBackground
: Optional callback for background notifications - must be a top level function
Returns true if initialization was successful.
Example:
final config = SuperFCMConfig(
appId: 'your-app-id',
);
await SuperFCM.instance.initialize(
config,
onOpened: (message) => print('Opened: ${message.messageId}'),
onForeground: (message) => print('Received: ${message.messageId}'),
);
Implementation
Future<bool> initialize(
SuperFCMConfig config, {
NotificationCallback? onOpened,
NotificationCallback? onForeground,
NotificationCallback? onBackground,
}) async {
logger.i('Initializing SuperFCM');
if (initialized) {
logger.d('SuperFCM already initialized');
return true;
}
_config = config;
_onForegroundCallback = onForeground;
_onOpenedCallback = onOpened;
logger.setLevel(config.logLevel);
// Initialize session manager
await SessionManager.instance.initialize(
config,
onSessionCountChanged: _handleSessionCountChanged,
);
await CacheManager.instance.initialize(config);
await RequestManager.instance.initialize(config);
await FirebaseMessagingService.instance.initialize(
onForeground: _handleForegroundMessage,
onOpened: _handleMessageOpenedApp,
onBackground: onBackground ?? _superFCMBackgroundHandler,
onTokenRefresh:
config.shouldMonitorTokenChange ? _handleTokenChange : null,
);
initialized = true;
logger.d('SuperFCM initialization complete');
subscription = await _getSubscriptionFromCache(_prefs);
if (subscription == null) {
logger.d('No subscription found locally, registering new subscription');
}
await _getSubscriptionFromServer();
if (subscription == null) {
logger.w('Unable to retrieve subscription from server');
ConnectionManager.instance.onConnected(_getSubscriptionFromServer);
}
if (subscription != null) {
await _processPendingOperations();
}
return initialized;
}