This commit is contained in:
Jonatas Antunes Messias 2024-06-19 17:44:14 -03:00
parent 29f82e89f9
commit 54cb542f68
2 changed files with 41 additions and 21 deletions

File diff suppressed because one or more lines are too long

View File

@ -137,40 +137,60 @@ class PushNotificationService {
}
Future<void> _updateDeviceToken() async {
ApiCallResponse? updToken;
final NotificationSettings settings =
await _requestNotificationPermission();
await _fetchAndLogApnsToken(settings);
///
NotificationSettings settings =
final String? deviceToken = await FirebaseMessaging.instance.getToken();
if (deviceToken == null) {
debugPrint('Failed to get Firebase Messaging token');
return;
}
debugPrint('Push Messaging token: $deviceToken');
FFAppState().token = deviceToken;
final ApiCallResponse? updTokenResponse =
await _updateTokenOnServer(deviceToken);
if (_isTokenUpdateSuccessful(updTokenResponse)) {
debugPrint('Token updated successfully');
} else {
debugPrint('Error updating token');
}
}
Future<NotificationSettings> _requestNotificationPermission() async {
final NotificationSettings settings =
await _firebaseMessaging.requestPermission();
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
debugPrint('User granted permission');
String? apnsToken = await _firebaseMessaging.getAPNSToken();
} else {
debugPrint('User declined or has not accepted permission');
}
return settings;
}
Future<void> _fetchAndLogApnsToken(NotificationSettings settings) async {
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
final String? apnsToken = await _firebaseMessaging.getAPNSToken();
if (apnsToken != null) {
debugPrint('APNS Token: $apnsToken');
} else {
debugPrint('Failed to get APNS token');
}
} else {
debugPrint('User declined or has not accepted permission');
}
String? deviceToken = await FirebaseMessaging.instance.getToken();
}
FFAppState().token = deviceToken!;
updToken = await PhpGroup.updToken.call(
token: FFAppState().token,
Future<ApiCallResponse?> _updateTokenOnServer(String deviceToken) async {
return await PhpGroup.updToken.call(
token: deviceToken,
devid: FFAppState().devUUID,
useruuid: FFAppState().userUUID,
);
if (PhpGroup.updToken.error(
(updToken.jsonBody ?? ''),
) ==
false) {
debugPrint('Token atualizado com sucesso');
debugPrint("Device Token: $deviceToken");
} else {
debugPrint('Erro ao atualizar token');
}
debugPrint('Push Messaging token: $deviceToken');
}
bool _isTokenUpdateSuccessful(ApiCallResponse? response) {
return PhpGroup.updToken.error((response?.jsonBody ?? '')) == false;
}
void _showNotification(RemoteMessage message) async {