From 29f82e89f95ccafa98a53d9ba23b7bb1c60263ab Mon Sep 17 00:00:00 2001 From: Jonatas Antunes Messias Date: Wed, 19 Jun 2024 17:19:45 -0300 Subject: [PATCH] move update token to homepage adn get APNSToken --- lib/actions/actions.dart | 17 -------- .../pushNotificationService.dart | 39 +++++++++++++++++-- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/actions/actions.dart b/lib/actions/actions.dart index 0e11964e..73845ec9 100644 --- a/lib/actions/actions.dart +++ b/lib/actions/actions.dart @@ -121,23 +121,6 @@ Future singInActionApp( (loginCall.jsonBody ?? ''), )!; // FFAppState().token = await FirebaseMessaging.instance.getToken(); - ApiCallResponse? updToken; - String? deviceToken = await FirebaseMessaging.instance.getToken(); - FFAppState().token = deviceToken!; - updToken = await PhpGroup.updToken.call( - token: FFAppState().token, - devid: FFAppState().devUUID, - useruuid: FFAppState().userUUID, - ); - if (PhpGroup.updToken.error( - (loginCall.jsonBody ?? ''), - ) == - false) { - debugPrint('Token atualizado com sucesso'); - debugPrint("Device Token: $deviceToken"); - } else { - debugPrint('Erro ao atualizar token'); - } FFAppState().createdAt = dateTimeFormat( 'd/M/y H:mm:ss', getCurrentTimestamp, diff --git a/lib/backend/push_notification/pushNotificationService.dart b/lib/backend/push_notification/pushNotificationService.dart index 2de93284..93be3a6c 100644 --- a/lib/backend/push_notification/pushNotificationService.dart +++ b/lib/backend/push_notification/pushNotificationService.dart @@ -1,5 +1,7 @@ import 'dart:async'; import 'dart:convert'; +import 'package:f_r_e_hub/app_state.dart'; +import 'package:f_r_e_hub/backend/api_requests/api_calls.dart'; import 'package:flutter/material.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; @@ -135,9 +137,40 @@ class PushNotificationService { } Future _updateDeviceToken() async { - String? token = await _firebaseMessaging.getToken(); - debugPrint('Push Messaging token: $token'); - // Send token to your server if required + ApiCallResponse? updToken; + + /// + NotificationSettings settings = + await _firebaseMessaging.requestPermission(); + if (settings.authorizationStatus == AuthorizationStatus.authorized) { + debugPrint('User granted permission'); + 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, + 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'); } void _showNotification(RemoteMessage message) async {