registerTokenForPushNotification static method
Future<void>
registerTokenForPushNotification(
- String token, {
- required dynamic onSuccess(
- String response
- required dynamic onError(
- CometChatException excep
Registers the obtained FCM Token which will be used by the CometChat server to send message via push notification.
method could throw PlatformException with error codes specifying the cause Registers a device token for push notifications.
Migration Note: Migrated from platform channels to native Dart implementation. Uses PushRepository for token registration. Behavior and signature remain identical for backward compatibility.
Android Reference: CometChat.registerTokenForPushNotification(String token, Settings.PushPlatforms platform, CallbackListener<String>)
Implementation
static Future<void> registerTokenForPushNotification(String token,
{required Function(String response)? onSuccess,
required Function(CometChatException excep)? onError}) async {
try {
// Validate token
if (token.isEmpty) {
onError?.call(CometChatException(
ErrorCode.errorEmptyPushToken,
ErrorMessage.errorMessageEmptyPushToken,
ErrorMessage.errorMessageEmptyPushToken));
return;
}
// Get SDK instance
final sdk = SdkRegistry.getInstance();
// Determine platform (fcm for Android, apns for iOS)
// On web, default to fcm
String platformType = 'fcm';
if (!kIsWeb) {
platformType =
defaultTargetPlatform == TargetPlatform.iOS ? 'apns' : 'fcm';
}
// Call native Dart push repository
final result = await sdk.push.registerToken(token, platformType);
// Call success callback
if (onSuccess != null) onSuccess(result);
} on SdkException catch (sdkEx) {
// Convert SdkException to CometChatException
final cometChatEx = CometChatException(
sdkEx.code,
sdkEx.details ?? sdkEx.message,
sdkEx.message,
);
_errorCallbackHandler(cometChatEx, null, null, onError);
} catch (e) {
_errorCallbackHandler(null, null, e, onError);
}
}