0

Hey I am trying to send notification inside from my flutter app to another app. But could not find anything. How can i implement this? Please help me. Thank you.

Sunny Rahi
  • 99
  • 1
  • 6
  • *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 Android/iOS 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 Nov 28 '21 at 15:06

2 Answers2

3

Using Cloud Functions (Recommended)

You can create a callable cloud function that sends an FCM notification.

exports.sendNotif = functions.https.onCall(async (data, context) => {
  // Your notification sending code here, something like
  // await admin.messaging().sendMulticast({data: ..., tokens: ...});
});

use cloud_function package from pub.dev to call this function from your flutter app.

final callable = FirebaseFunctions.instance.httpsCallable('sendNotif');
await callable();

Using Service Accounts (Not Recommended)

  1. Go to Project Settings (the cog icon) -> Service Accounts in your Firebase console.
  2. Click on "Manage service account permissions".
  3. Add a new service account and fill in the information needed.
  4. In the roles section, give it the required access to FCM, for example by selecting "Firebase Cloud Messaging Admin".
  5. Now you can see your newly created service account, press on the actions (triple dot), and select manage keys.
  6. Create a new JSON key and download the file. This file contains sensitive information such as the private key.
  7. Now you can act as a service account from your flutter code by authenticating using the JSON file. More info here.

You can see why this is not a viable option, you need to have the service account JSON key shipped with the flutter app, so theoretically any user can easily send any notifications by getting their hands on the key.

Using the cloud functions (or a server) approach lets you specify the things that should be sent and who can send them.

Hossein Yousefi
  • 889
  • 8
  • 18
  • I want to do it locally without using any server or cloud function. – Sunny Rahi Nov 28 '21 at 14:10
  • I wouldn't recommend you to do it but you can also create a service account and directly create the FCM notification from the app. I'll update my answer to include more details. – Hossein Yousefi Nov 28 '21 at 14:11
  • Thank you for your response. I looks very complected to me. I am noob. Hey did you know any other way such using the rest api. I found this documentation but could not figure out how to call out https://firebase.flutter.dev/docs/messaging/notifications#via-rest – Sunny Rahi Nov 28 '21 at 14:46
  • 1
    @SunnyRahi You can absolutely send the notification via rest but you still need to be authorized by passing in the `Authorization` header (as shown in the example) otherwise anyone could send a notification, right? In order to get the token you should do something like the approach I've written. – Hossein Yousefi Nov 28 '21 at 14:50
  • I have created the key. Now how can i use it for sending notification from the app. The documentation look complicated for me. can you help me please? – Sunny Rahi Nov 28 '21 at 15:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239648/discussion-between-sunny-rahi-and-hossein-yousefi). – Sunny Rahi Nov 28 '21 at 15:20
1

You should have a server that connects to the firebase account that you want then get your request from the first app and send a notification to the app and token that you want.

Mojtaba Ghiasi
  • 823
  • 6
  • 16