Search code examples
firebasegoogle-cloud-firestorewebrtcvoip

How to notify the user about the offer call in flutter?


My apps needs a VOIP call functionality and I use webRTC to achieve it. In webRTC how the reciever knows about the incoming call?

All my users will register in Django and flutter as a frontend. If I use FCM how can specify the exact user to send notifcation. Some articles suggest to use UID, email and such things if I have been authenticated with the firebase I might know about the UID but I use my own server How to make this possible?

In case if we use email to send a notification i.e., will firebase send the notification to the particular user?


Solution

  • Use firebase cloud messaging. If you are using Django or whatever server for the backend you just have to get the user's fcm token while user registers from the app. And in your database store the user's email with that token. So whenever you want to send a notification to a specific user you can trigger by their respective fcm token.

    Use below code to get user's token in flutterfire.

    FirebaseMessaging messaging = FirebaseMessaging.instance;
    
    // use the returned token to send messages to users from your custom server
    String token = await messaging.getToken(
      vapidKey: "BGpdLRs......",
    );
    

    And to send a notification to that user via your Django backend make a post request like below.

    Use a service api.

    URL: https://fcm.googleapis.com/fcm/send
    
    Method Type: POST
    

    Headers:

    Content-Type: application/json
    Authorization: key=your api key
    

    Body/Payload:

    { 
     "notification": {
        "title": "Your Title",
        "text": "Your Text",
      },
        "data": {
        "keyname": "any value" // Any data you want to send for your call initialization 
        },
      "to" : "Token you got"
    }