I was exploring the way to implement the notification in Flutter. I know we can send the notification using Firebase Console & using the Cloud Functions.
But I got to know about this method of sending the notification directly from flutter app based on action. Just wanted to know more about this, Is it a good way of sending the notification? Can I use this method to send multiple notifications in my production app.
import 'dart:convert';
import 'package:http/http.dart';
import 'package:meta/meta.dart';
class Messaging {
static final Client client = Client();
// from 'https://console.firebase.google.com'
// --> project settings --> cloud messaging --> "Server key"
static const String serverKey =
'SERVER_KEY';
static Future<Response> sendToAll({
@required String title,
@required String body,
}) =>
sendToTopic(title: title, body: body, topic: 'all');
static Future<Response> sendToTopic(
{@required String title,
@required String body,
@required String topic}) =>
sendTo(title: title, body: body, fcmToken: '/topics/$topic');
static Future<Response> sendTo({
@required String title,
@required String body,
@required String fcmToken,
}) =>
client.post(
'https://fcm.googleapis.com/fcm/send',
body: json.encode({
'notification': {'body': '$body', 'title': '$title'},
'priority': 'high',
'data': {
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done',
},
'to': '$fcmToken',
}),
headers: {
'Content-Type': 'application/json',
'Authorization': 'key=$serverKey',
},
);
}