Firebase Notification send - firebase_messaging

 install the firebase_messaging:

flutter pub add firebase_messaging

set permissions on AndroidManifest file

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="android.permission.MANAGE_NOTIFICATIONS"/>

notification_methods.dart

import 'dart:convert';
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:http/http.dart';



class NotificationMethods {
  final CollectionReference usersCollection =
      FirebaseFirestore.instance.collection("users");
  FirebaseMessaging fMessaging = FirebaseMessaging.instance;

  //getting the token and save to firebase firestore
  Future<void> getFMToken() async {
    await fMessaging.requestPermission();
    String? token = await fMessaging.getToken();
    if (token != null) {
      try {
        String uid = FirebaseAuth.instance.currentUser!.uid;
        await usersCollection.doc(uid).update({
          "token": token,
        });
      } catch (e) {}
    }
  }

  Future<void> sendPushNotification(
      String token,  String user) async {
    try {
      final body = {
        "to": token,
        "notification": {
          "title": "Your message!",
          "body": "From $user",
          "android_channel_id": "chats"
        },
      };

      var url = Uri.https('example.com', 'whatsit/create');
      var response =
          await post(Uri.parse("https://fcm.googleapis.com/fcm/send"),
              headers: {
                HttpHeaders.contentTypeHeader: "application/json",
                HttpHeaders.authorizationHeader:
                    "key=//give key"
              },
              body: jsonEncode(body));
      print('Response status: ${response.statusCode}');
      print('Response body: ${response.body}');
    } catch (e) {
      print(e.toString());
    }
  }
}


When user come to login screen, you should renew the token. then in Home screen init use this


  Future initData() async {
    await NotificationMethods().getFMToken();
  }

  @override
  void initState() {
    initData();
    super.initState();
   
  }


Ok, now you have to find 

{
                HttpHeaders.contentTypeHeader: "application/json",
                HttpHeaders.authorizationHeader:
                    "key=%thiskey%"
              },

(in notification methods,)

go to Google Cloud console

find 


enable it. 

Now, go to 

you can see the key

add it.

and install 
 flutter pub add firebase_analytics

add this to main
 await FirebaseMessaging.instance.getInitialMessage();

Now, finish

 await NotificationMethods().sendPushNotification(
                'c1A3Y4deTTeH0-RLUlqe5Z:APA91bHakRC63LHQ4q3-zIFDT1rVTPPUQbVfZivi-Ke1LxsArfSjoXVGTH7aoggHq2VuK-qPvhDUygfBhgr18pGT5J1BAcmCDHDgthakFU3YE4cK7WFzCwC7fRsK4crpTS-TO4KIZ5i7',
                'tes');

Comments