46.Send Push Notification | Firebase Messaging Flutter | Chat App

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 49

  • @harshRajpurohit
    @harshRajpurohit  11 หลายเดือนก่อน +3

    For anyone facing the issue with the notifications.
    Credit Goes to Mafaz Khan for this Research.
    Observation 1: The notifications API needs to be migrated to v1
    Fix:
    1. Change URL to this fcm.googleapis.com/v1/projects/PROJECTID/messages:send
    2. Obtain your project id from firebase-> project settings -> general (Here you can find your project ID.
    Observation 2: Your server key is no longer needed
    Fix:
    1. Instead go to your Google cloud console(This is the page from where you generated the API key.
    2. Search for service account, then click for your current project (It should liik like this "firebase-adminsdk-sz6nt@YOURPROJECTNAME-87ebc.iam.gserviceaccount.com"
    3. Once you click on the account you will find tabs, now go to keys tab and create a new key(This should download a json file)
    4. Now open the json file and copy the whole code as this will generate our bearer token
    Observation 3: We need extra packages and coding to generate bearer token
    Fix:
    1. First add this package (googleapis_auth:) to your pubspec.yaml.
    2 Now the coding part, create a new file in apis folder and name it access_firebase_token.dart.
    3. Copy and paste the below code:
    Code:
    import 'package:googleapis_auth/auth_io.dart';
    class AccessFirebaseToken {
    static String fMessagingScope =
    "www.googleapis.com/auth/firebase.messaging";
    Future getAccessToken() async {
    final client = await clientViaServiceAccount(
    ServiceAccountCredentials.fromJson(
    {
    PASTE YOUR GENERATED JSON FILE CODE OVER HERE, BE MINDFUL TO NOT COPY THE CURLY BRACES JUST PASTE THE CODE IN HERE AND IMPORT THE ERRORS
    }
    ),
    [fMessagingScope],
    );
    final accessToken = client.credentials.accessToken.data;
    return accessToken;
    }
    }
    Observation 4: The code needs to be changes for sendPushNotifcation function
    Fix:
    1. Just copy the code below and imprt the errors
    Code:
    static Future sendPushNotification(
    ChatUser chatUser, String msg) async {
    AccessFirebaseToken accessToken = AccessFirebaseToken();
    String bearerToken = await accessToken.getAccessToken();
    final body = {
    "message": {
    "token": chatUser.pushToken,
    "notification": {
    "title": me.name,
    "body": msg
    },
    }
    };
    try {
    var res = await post(
    Uri.parse('fcm.googleapis.com/v1/projects/YOURPROJECTID/messages:send'),
    headers: {
    "Content-Type": "application/json",
    'Authorization': 'Bearer $bearerToken'
    },
    body: jsonEncode(body),
    );
    print("Response statusCode: ${res.statusCode}");
    print("Response body: ${res.body}");
    } catch (e) {
    print("
    sendPushNotification: $e");
    }
    }
    Check for notification and it will work😊
    Official Docs: firebase.google.com/docs/cloud-messaging/migrate-v1

    • @antongrekov7710
      @antongrekov7710 11 หลายเดือนก่อน

      what

    • @HUTIENLAN
      @HUTIENLAN 7 หลายเดือนก่อน

      would you like to make a new video to this part?? huge thanks

    • @harshRajpurohit
      @harshRajpurohit  7 หลายเดือนก่อน

      I have already updated my github codes for it and added some comments too about how to do it (if you wish then you can refer to it).

    • @sohaiburrehman7257
      @sohaiburrehman7257 6 หลายเดือนก่อน

      What is obtaining from this code?
      Sever key?
      Because the Google console is not opening so i can not enable it .
      And i don't have the server key

  • @AmreshKumar-bc7ev
    @AmreshKumar-bc7ev 2 ปีที่แล้ว +2

    In TH-cam i have seen lots of video but sir your explanation superb ❤️👍👍

  • @z_verseau
    @z_verseau 4 หลายเดือนก่อน

    Hi sir, why we don't use API Firebase Cloud Messaging (V1). API Cloud Messaging (legacy) don't work

    • @harshRajpurohit
      @harshRajpurohit  4 หลายเดือนก่อน

      You can refer to the pinned comment of this video about how to migrate to the latest way.
      Or you can also refer to my updated github codes as per new api.
      (I've added some necessary comments as well).

  • @jingyipua773
    @jingyipua773 ปีที่แล้ว

    May I ask is the app noti still able be triggerred when the app is being terminated (closed)?

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      Yep, push notifications has nothing to do with app running in the background.

    • @jingyipua773
      @jingyipua773 ปีที่แล้ว

      @@harshRajpurohit sorry, my meaning is the app is not running at all. There are three state of app:
      -active (in app)
      -inactive (background)
      -terminated (app is exit and closed)
      Will your method able to handle push noti on three app states?

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      Mainly it handles for terminated & somewhat for background state but for active state we need to use local_notification package and listen for notification receive and then show our custom notification.

  • @parasverma7713
    @parasverma7713 ปีที่แล้ว

    Sir mene apne mobile jb ye sb krke app run kra to ek push token generate hua or us account me merko notifications aari thi and fir mene pehle wale account ko log out kiya or dusre account se login kiya fir v same hi pushtoken generate hua esa ku? Or ye firebase se jb aapne test notification bheji to firebase ko pta kese chala ki konse account me notification bhejni h ? Firebase jo hamara user login h app se uska pushtoken read krke use notification bhej rha hai kya? Or agr wo push token read kr rha h user ka to kese kuki hmne to koi code ni likha push token read krne ka

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      Regarding the same push token: Actually on a single device generally the same push token is generated.
      Regarding test notification: We're passing push token while sending test notification i.e. how firebase knows whom to send.

  • @luisrieker3547
    @luisrieker3547 9 หลายเดือนก่อน

    why is my app logo only a white circle, although i changed it in AndroidManifest and image is transparent

    • @harshRajpurohit
      @harshRajpurohit  9 หลายเดือนก่อน

      It's a bit difficult to say why it's happening rather you can refer to the article below for more details of how to change it.
      stackoverflow.com/questions/46676014/how-to-change-the-android-notification-icon-status-bar-icon-for-push-notificatio

  • @FirebaseAcount
    @FirebaseAcount 7 หลายเดือนก่อน

    🔥🔥🔥🔥🔥 can anyone tell me how can i add a custom notification sound when receive messages in background, currently playing phones default sound? thank you very much 🔥🔥🔥🔥🔥

  • @AmreshKumar-bc7ev
    @AmreshKumar-bc7ev 2 ปีที่แล้ว

    Sir main iss WeChat project main mannual registration bhi karna chahta hu to chatUser I'd time & is_online kaise request kar shakte ??

    • @harshRajpurohit
      @harshRajpurohit  2 ปีที่แล้ว

      Actually whether you implemented google sign in or any other registration method (and you can use anything as id like email to), you just need to setup firebase cloud firestore and you can create and update any filed you like.

  • @sohaiburrehman7257
    @sohaiburrehman7257 6 หลายเดือนก่อน

    I am unable to generate the server key
    The google console giving me error

  • @e.n.d5103
    @e.n.d5103 ปีที่แล้ว

    I have followed until the end but why is the notification not appearing and why is the permission request also not appearing?

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว +2

      Actually permission request is for Android 13 or higher, for lower androids it works without it.
      Have you tried sending test notification from firebase using your device id.

  • @ramakantpandit8507
    @ramakantpandit8507 ปีที่แล้ว

    sir i am having this error 'Some input files use or override a deprecated API.' and nothing is being shown in the push notification log

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      Deprecated api error is okay but push notification should show some logs if you've printed it. Maybe an error or something.
      Have a look at it calmly.

  • @dhokiyaharsh
    @dhokiyaharsh 5 หลายเดือนก่อน

    Sir, Cloud Messaging API Disabled? How to Send Push Notification And how to get server key

    • @harshRajpurohit
      @harshRajpurohit  5 หลายเดือนก่อน

      You can refer to the pinned comment of this video about how to migrate to the latest way or you can also refer to my updated github codes as per new api (I've added some necessary comments as well).

  • @tricaricoenrico
    @tricaricoenrico ปีที่แล้ว +1

    Hi, good video but api legacy are deprecated

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว +1

      Yep, I got to know.
      You can try referring to the first pinned comment for doc reference about how to migrate.

  • @arnabsadhukhan89
    @arnabsadhukhan89 ปีที่แล้ว

    Bhaiya mujhe yea error dikha raha hai
    [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field 'pushToken' has already been initialized.
    pls help kijiye😩😩

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      If you've written the final in front of pushToken variable in the chat_user model then just remove it.

    • @arnabsadhukhan89
      @arnabsadhukhan89 ปีที่แล้ว

      @@harshRajpurohit Thanks a lot bhaiya❤👍

  • @tushiyahsolutions8000
    @tushiyahsolutions8000 ปีที่แล้ว +1

    Do you have this tutorial in English?

  • @fahadislam8975
    @fahadislam8975 ปีที่แล้ว

    sir ap na jo api postman mai test ki wo kha sa li

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      Actually I got it after searching a lot on google and stacks overflow.

  • @gautamsolanki3644
    @gautamsolanki3644 10 หลายเดือนก่อน

    please aap ese project banate rahi ye or ek project banaye with rest api all crud operation cover pleasse

  • @kendallfernandez9410
    @kendallfernandez9410 7 หลายเดือนก่อน

    ere un makina

  • @ankitpurohit18
    @ankitpurohit18 2 ปีที่แล้ว +1

    👍👍👍👍👍

  • @deepakdalai9905
    @deepakdalai9905 2 ปีที่แล้ว

    🙂

  • @adilhesenov2962
    @adilhesenov2962 ปีที่แล้ว

    perfect

  • @yasirgul914
    @yasirgul914 ปีที่แล้ว

    E/FirebaseInstanceId(20797): Topic sync or token retrieval failed on hard failure exceptions: PHONE_REGISTRATION_ERROR. Won't retry the operation.
    E/LB (20797): fail to open file: No such file or directory
    E/FirebaseInstanceId(20797): Topic sync or token retrieval failed on hard failure exceptions: PHONE_REGISTRATION_ERROR. Won't retry the operation.
    E/flutter (20797): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_messaging/unknown] java.io.IOException: PHONE_REGISTRATION_ERROR

    • @harshRajpurohit
      @harshRajpurohit  ปีที่แล้ว

      Try changing the version of google gsm in build.gradle (inside android/app folder) to 4.3.15 (maybe it can work) .
      Line to update: classpath 'com.google.gms:google-services:4.3.15'

  • @FirebaseAcount
    @FirebaseAcount 7 หลายเดือนก่อน

    🔥🔥🔥🔥🔥 can anyone tell me how can i add a custom notification sound when receive messages in background, currently playing phones default sound? thank you very much 🔥🔥🔥🔥🔥

    • @harshRajpurohit
      @harshRajpurohit  7 หลายเดือนก่อน

      Actually you have to do it separately for android and iOS but you can refer to the article below:
      stackoverflow.com/questions/54002617/custom-sound-push-notification-does-not-work-flutter/54003722#54003722
      (I know it's a bit complex but during its implementation, you will learn a lot).