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 {
if (pushPlatforms == PushPlatforms.FCM_FLUTTER_IOS ||
pushPlatforms == PushPlatforms.FCM_FLUTTER_ANDROID) {
if (Platform.isIOS &&
pushPlatforms == PushPlatforms.FCM_FLUTTER_ANDROID) {
_errorCallbackHandler(
CometChatException(
CometChatNotificationErrorCode.ERROR_INVALID_PUSH_PLATFORM,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_PUSH_PLATFORM,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_PUSH_PLATFORM),
null,
null,
onError);
return null;
} else {
if (fcmToken == null) {
_errorCallbackHandler(
CometChatException(
CometChatNotificationErrorCode.ERROR_INVALID_FCM_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_FCM_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_INVALID_FCM_TOKEN),
null,
null,
onError);
return null;
}
}
} else if (pushPlatforms == PushPlatforms.APNS_FLUTTER_DEVICE) {
if (deviceToken == null) {
_errorCallbackHandler(
CometChatException(
CometChatNotificationErrorCode
.ERROR_INVALID_APNS_DEVICE_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_DEVICE_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_DEVICE_TOKEN),
null,
null,
onError);
return null;
}
} else if (pushPlatforms == PushPlatforms.APNS_FLUTTER_VOIP) {
if (voipToken == null) {
_errorCallbackHandler(
CometChatException(
CometChatNotificationErrorCode.ERROR_INVALID_APNS_VOIP_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_VOIP_TOKEN,
CometChatNotificationErrorMessages
.ERROR_MESSAGE_APNS_INVALID_VOIP_TOKEN),
null,
null,
onError);
return null;
}
}
final result = await channel.invokeMethod('registerPushToken', {
"pushPlatforms": pushPlatforms.type,
"fcmToken": fcmToken,
"deviceToken": deviceToken,
"voipToken": voipToken,
"providerId": providerId
});
if (onSuccess != null) onSuccess(result);
return result;
} on PlatformException catch (p) {
_errorCallbackHandler(null, p, null, onError);
} catch (e) {
_errorCallbackHandler(null, null, e, onError);
}
return null;
}