import 'package:googleapis_auth/auth_io.dart'; // access token almak için import 'package:http/http.dart' as http; Future<void> bildirimGonder(String title, String body, String toToken) async { // 1. Service account dosyasını oku String jsonString = await rootBundle.loadString('assets/ayar/firebaseprojeid.json'); Map<String, dynamic> serviceAccount = json.decode(jsonString); // 2. JSON içinden bilgiler String clientEmail = serviceAccount['client_email']; String privateKey = serviceAccount['private_key']; final accountCredentials = ServiceAccountCredentials( clientEmail, ClientId("", ""), // boş bırak, sadece ServiceAccount kullanıyoruz privateKey, ); final scopes = ['https://www.googleapis.com/auth/firebase.messaging']; final authClient = await clientViaServiceAccount(accountCredentials, scopes); final accessToken = authClient.credentials.accessToken.data; print('Access Token: $accessToken'); // 3. Bildirimi gönder await http.post( Uri.parse('https://fcm.googleapis.com/v1/projects/${serviceAccount["project_id"]}/messages:send'), headers: { 'Authorization': 'Bearer $accessToken', 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode({ "message": { "token": toToken, "notification": { "title": title, "body": body, } } }), ); } |