0

I use firebase messaging to push notifications.

Now i push notifications from console to users on app, but I want the user to push notifications from app to another user when send message. How can I do that?

Knowing that I have saved the device ID of each user on Firestore and the ID is renewed every time the user logs into the application.

I tried using the following code to send notifications in case the user sends a message, but it didn't work

sendNotification(String body, String idToken) async {
    await http.post(
      (Uri.parse('https://fcm.googleapis.com/fcm/send')),
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Authorization': 'key=$servitoken',
      },
      body: jsonEncode(
        <String, dynamic>{
          'notification': <String, dynamic>{
            'body': "${body.toString()}",
            'title': "!"
          },
          'priority': 'high',
          'data': <String, dynamic>{
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done'
          },
          'to': await "${idToken}",
        },
      ),
    );
  }

,##

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You're trying to trigger notifications from the mobile app? – Denzel Sep 24 '22 at 17:25
  • 1
    *firebaser here* Calls to the FCM REST API require that you specify the FCM *server** key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. By including this key in your client-side Flutter app, a malicious user can find it and you're putting your users at risk. See https://stackoverflow.com/a/37993724 for a better solution. – Frank van Puffelen Sep 24 '22 at 18:17

1 Answers1

0

I will recommend you to use onesignal instead of just using firebase cloud messaging. Onesignal is easy to use and can make your development life more

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 04 '22 at 12:46