Initial commit
This commit is contained in:
127
lib/constant/send_notification.dart
Normal file
127
lib/constant/send_notification.dart
Normal file
@@ -0,0 +1,127 @@
|
||||
// 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<String> getAccessToken() async {
|
||||
Map<String, dynamic> 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<bool> sendFcmMessage(String type, String token, Map<String, dynamic>? 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: <String, String>{
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $accessToken',
|
||||
},
|
||||
body: jsonEncode(
|
||||
<String, dynamic>{
|
||||
'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<bool> sendOneNotification({required String token, required String title, required String body, required Map<String, dynamic> 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: <String, String>{
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $accessToken',
|
||||
},
|
||||
body: jsonEncode(
|
||||
<String, dynamic>{
|
||||
'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<bool> sendChatFcmMessage(String title, String message, String token, Map<String, dynamic>? 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: <String, String>{
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $accessToken',
|
||||
},
|
||||
body: jsonEncode(
|
||||
<String, dynamic>{
|
||||
'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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user