0

I know that we can use firebase cloud messaging and from the firebase console only we can generate notifications for flutter app (after we setup up everything in the app ). But is there any way by which I can generate notification on press of a button. For e.g. The admin of the app adds a new event, as soon as he/she presses [Add Event] button, a notification gets generated for each user (using firebase) that a new event has been added? How can this be achieved?

Manny
  • 21
  • 5
  • you can do that using callable functions or thru http. To learn more about it, check [this](https://firebase.google.com/docs/cloud-messaging/http-server-ref) – john Nov 02 '22 at 09:59
  • 1
    There is no way to securely send messages from client to client through the FCM API. You will always need a trusted environment for that, such as your development machine, a server that you control, or Cloud Functions/Cloud Run. See https://stackoverflow.com/a/37993724 for that solution. – Frank van Puffelen Nov 02 '22 at 14:29

1 Answers1

2

You can call your server in onPressed: (https://docs.flutter.dev/cookbook/networking/fetch-data)

Example:

onPressed: () async {
  await http.get(Uri.parse("https://your-server.com/add-event"));
}

then from your server you can send a fcm notfication to the devices: (https://firebase.google.com/docs/cloud-messaging/server#implementing-the-xmpp-connection-server-protocol)

Example:

const registrationTokens = [
  'YOUR_REGISTRATION_TOKENS',
];

const message = {
  data: {new_event: 1},
  tokens: registrationTokens,
};

getMessaging().sendMulticast(message)
  .then((response) => {
    console.log(response.successCount + ' messages were sent successfully');
  });
Supertommino
  • 562
  • 3
  • 13
  • Hi @Supertommino, thanks for answering. Can you please elaborate this a bit more, like how will calling the server in onPressed generate the notification? – Manny Nov 02 '22 at 09:03
  • 1
    Hi @Manny, I just added the examples, is it clearer now? :) – Supertommino Nov 02 '22 at 10:19