Back

Cannot send message with FCM

  • 0
  • Functions
jj_mad
29 Oct, 2023, 20:43

Hi AppWrite, I created a function from your templace (FCM template). I tried to send a message but I cannot go any further than getting an error 500 {"ok":false,"error":"Failed to send the message"}.

This is the curl request.

TypeScript
curl -X POST https://XXXXXXXXXXXX.appwrite.global \
-H "Content-Type: application/json" \
-d '{"deviceToken":"XXXXXXXXXX", "message": {
        "title": "Title",
        "body": "Hello World"}}'

Not sure if the problem is related with the the deviceToken or the settings of the function (FCM_CLIENT_EMAIL, FCM_DATABASE_URL, FCM_PROJECT_ID or FCM_PRIVATE_KEY)

I believe it is the later because I am using the deviceToken with the functions of firebase and I can perfectly receive messages. This is the code I use to send messages in directly in Firebase

TypeScript
return admin.messaging().sendToDevice(token, payload)
  .then(response => {
          console.log("Successfully sent message: ", response);
          return null
          })
          .catch(response => {
          console.log("Error sending message:", error);
          return null
        });

Hope someone that has it working can help me

Thank you for your time in advance

TL;DR
The user is experiencing an error when trying to send a message with FCM (Firebase Cloud Messaging). They have tried using the private_key and checked all FCM values, but the error persists. They are unsure if the issue is related to the deviceToken or the function settings. They provide a curl request and mention that they are able to successfully send messages using Firebase directly. Solution: It is suggested to verify the FCM_CLIENT_EMAIL, FCM_DATABASE_URL, FCM_PROJECT_ID, and FCM_PRIVATE_KEY values. Additionally, the issue could be related to the deviceToken or the function settings. It is recommended to compare the
D5
29 Oct, 2023, 21:04

Did you have created a token/key in the firebase dashboard and have set it?

jj_mad
29 Oct, 2023, 21:15

I went to service accounts, in project settings, Firebase Admin SDK->Generate new private key. Do you mean that? For FCM_PRIVATE_KEY I used the value private_key_id in the json generated as the other (private_key) starts with "-----BEGIN PRIVATE KEY-----\nMIIE....Z0ATl5yuc\n-----END PRIVATE KEY-----\n" and does not look similar to the one in the github readme.md

jj_mad
29 Oct, 2023, 21:20

I have tried with the private_key in case was that but still the same error. All the values FCM_CLIENT_EMAIL, FCM_DATABASE_URL, FCM_PROJECT_ID or FCM_PRIVATE_KEY are from the json except for the FCM_DATABASE_URL which I pick from the Project settings>Firebase Admin SDK inside the bracket admin.initializeApp({...}

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more