registerPushToken static method
Future<String?>
registerPushToken(})
Registers a push notification token with the CometChat server for the current user.
Depending on the provided PushPlatforms, this method registers the appropriate token such as FCM token for Android and iOS,
APNS device token, or APNS VOIP token for iOS.
It requires at least one valid token to be provided based on the platform.
If registration is successful, onSuccess is invoked with a success response.
If an error occurs, onError is called with a CometChatException.
Returns a Future<String?> that completes with a success response if the operation is successful, or null if an error occurs.
Implementation
static Future<String?> registerPushToken(PushPlatforms pushPlatforms,
{String? fcmToken,
String? deviceToken,
String? voipToken,
String? providerId,
Function(String response)? onSuccess,
Function(CometChatException e)? onError}) async {
try {
// Validate platform-specific tokens
if (pushPlatforms == PushPlatforms.FCM_FLUTTER_IOS ||
pushPlatforms == PushPlatforms.FCM_FLUTTER_ANDROID) {
if (Platform.isIOS &&
pushPlatforms == PushPlatforms.FCM_FLUTTER_ANDROID) {
_handleError(
CometChatException(
CometChatNotificationErrorCode.ERROR_INVALID_PUSH_PLATFORM,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_PUSH_PLATFORM,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_PUSH_PLATFORM),
onError);
return null;
} else {
if (fcmToken == null) {
_handleError(
CometChatException(
CometChatNotificationErrorCode.ERROR_INVALID_FCM_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_FCM_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_FCM_TOKEN),
onError);
return null;
}
}
} else if (pushPlatforms == PushPlatforms.APNS_FLUTTER_DEVICE) {
if (deviceToken == null) {
_handleError(
CometChatException(
CometChatNotificationErrorCode
.ERROR_INVALID_APNS_DEVICE_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_DEVICE_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_DEVICE_TOKEN),
onError);
return null;
}
} else if (pushPlatforms == PushPlatforms.APNS_FLUTTER_VOIP) {
if (voipToken == null) {
_handleError(
CometChatException(
CometChatNotificationErrorCode.ERROR_INVALID_APNS_VOIP_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_VOIP_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_VOIP_TOKEN),
onError);
return null;
}
}
// Determine the token to use based on platform
String token;
if (pushPlatforms == PushPlatforms.FCM_FLUTTER_ANDROID ||
pushPlatforms == PushPlatforms.FCM_FLUTTER_IOS) {
token = fcmToken!;
} else if (pushPlatforms == PushPlatforms.APNS_FLUTTER_DEVICE) {
token = deviceToken!;
} else {
token = voipToken!;
}
// Get current timezone as IANA ID to match Android SDK's
// TimeZone.getDefault().getID() behavior
final tzName = DateTime.now().timeZoneName;
final String timezone;
if (tzName.contains('/')) {
timezone = tzName;
} else {
final offset = DateTime.now().timeZoneOffset;
if (offset.inMinutes == 0) {
timezone = 'Etc/UTC';
} else {
final hours = -(offset.inMinutes / 60).round();
timezone = hours >= 0 ? 'Etc/GMT+$hours' : 'Etc/GMT$hours';
}
}
final sdk = SdkRegistry.getInstance();
await sdk.notificationsApi.registerPushToken(
token,
pushPlatforms.type,
timezone,
providerId: providerId,
);
const result = 'Token registered successfully';
if (onSuccess != null) onSuccess(result);
return result;
} catch (e) {
_handleError(e, onError);
}
return null;
}