initialize method
- required DarwinInitializationSettings settings,
- DidReceiveNotificationResponseCallback? onDidReceiveNotificationResponse,
- DidReceiveBackgroundNotificationResponseCallback? onDidReceiveBackgroundNotificationResponse,
Initializes the plugin for iOS.
Call this method on application before using the plugin further.
Accepts either DarwinInitializationSettings for basic Darwin-based configuration or IOSInitializationSettings for iOS-specific features like CarPlay notifications.
Initialisation may also request notification permissions where users will see a permissions prompt. This may be fine in cases where it's acceptable to do this when the application runs for the first time. However, if your application needs to do this at a later point in time, set the DarwinInitializationSettings.requestAlertPermission, DarwinInitializationSettings.requestBadgePermission and DarwinInitializationSettings.requestSoundPermission and DarwinInitializationSettings.requestProvisionalPermission values to false. requestPermissions can then be called to request permissions when needed.
When using IOSInitializationSettings, CarPlay notifications can be enabled by setting IOSInitializationSettings.requestCarPlayPermission to true. When using DarwinInitializationSettings, CarPlay is disabled by default.
The onDidReceiveNotificationResponse callback is fired when the user
selects a notification or notification action that should show the
application/user interface.
application was running. To handle when a notification launched an
application, use getNotificationAppLaunchDetails. For notification
actions that don't show the application/user interface, the
onDidReceiveBackgroundNotificationResponse callback is invoked on
a background isolate. Functions passed to the
onDidReceiveBackgroundNotificationResponse
callback need to be annotated with the @pragma('vm:entry-point')
annotation to ensure they are not stripped out by the Dart compiler.
Implementation
Future<bool?> initialize({
required DarwinInitializationSettings settings,
DidReceiveNotificationResponseCallback? onDidReceiveNotificationResponse,
DidReceiveBackgroundNotificationResponseCallback?
onDidReceiveBackgroundNotificationResponse,
}) async {
_onDidReceiveNotificationResponse = onDidReceiveNotificationResponse;
_channel.setMethodCallHandler(_handleMethod);
// Convert to map using appropriate mapper based on runtime type
// IOSInitializationSettings.toMap() automatically includes CarPlay field
// DarwinInitializationSettings.toMap() does not include CarPlay field
final Map<String, Object> arguments;
if (settings is IOSInitializationSettings) {
// Explicitly call iOS mapper extension
arguments = settings.toMap();
} else {
// Use Darwin mapper for DarwinInitializationSettings
arguments = settings.toMap();
}
_evaluateBackgroundNotificationCallback(
onDidReceiveBackgroundNotificationResponse,
arguments,
);
return await _channel.invokeMethod('initialize', arguments);
}