Home > database >  Is there any way to send FCM notification inside from flutter App?
Is there any way to send FCM notification inside from flutter App?

Time:11-29

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related