// ignore_for_file: non_constant_identifier_names import 'dart:convert'; import 'package:driver/constant/constant.dart'; import 'package:driver/models/notification_model.dart'; import 'package:driver/utils/fire_store_utils.dart'; import 'package:flutter/cupertino.dart'; import 'package:googleapis_auth/auth_io.dart'; import 'package:http/http.dart' as http; class SendNotification { static final _scopes = ['https://www.googleapis.com/auth/firebase.messaging']; static Future getCharacters() { return http.get(Uri.parse(Constant.jsonNotificationFileURL.toString())); } static Future getAccessToken() async { Map jsonData = {}; await getCharacters().then((response) { jsonData = json.decode(response.body); }); final serviceAccountCredentials = ServiceAccountCredentials.fromJson(jsonData); final client = await clientViaServiceAccount(serviceAccountCredentials, _scopes); return client.credentials.accessToken.data; } static Future sendFcmMessage(String type, String token, Map? payload) async { print(type); try { final String accessToken = await getAccessToken(); debugPrint("accessToken=======>"); debugPrint(accessToken); NotificationModel? notificationModel = await FireStoreUtils.getNotificationContent(type); final response = await http.post( Uri.parse('https://fcm.googleapis.com/v1/projects/${Constant.senderId}/messages:send'), headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer $accessToken', }, body: jsonEncode( { 'message': { 'token': token, 'notification': {'body': notificationModel!.message ?? '', 'title': notificationModel.subject ?? ''}, 'data': payload, } }, ), ); debugPrint("Notification=======>"); debugPrint(response.statusCode.toString()); debugPrint(response.body); return true; } catch (e) { debugPrint("Notification error :::::::::: ${e.toString()}"); return false; } } static Future sendOneNotification({required String token, required String title, required String body, required Map payload}) async { try { final String accessToken = await getAccessToken(); debugPrint("accessToken=======>"); debugPrint(accessToken); final response = await http.post( Uri.parse('https://fcm.googleapis.com/v1/projects/${Constant.senderId}/messages:send'), headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer $accessToken', }, body: jsonEncode( { 'message': { 'token': token, 'notification': {'body': body, 'title': title}, 'data': payload, } }, ), ); debugPrint("Notification=======>"); debugPrint(response.statusCode.toString()); debugPrint(response.body); return true; } catch (e) { debugPrint(e.toString()); return false; } } static Future sendChatFcmMessage(String title, String message, String token, Map? payload) async { try { final String accessToken = await getAccessToken(); final response = await http.post( Uri.parse('https://fcm.googleapis.com/v1/projects/${Constant.senderId}/messages:send'), headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer $accessToken', }, body: jsonEncode( { 'message': { 'token': token, 'notification': {'body': message, 'title': title}, 'data': payload, } }, ), ); debugPrint("Notification=======>"); debugPrint(response.statusCode.toString()); debugPrint(response.body); return true; } catch (e) { print(e); return false; } } }