queelag / fcm

MIT License
6 stars 2 forks source link

Error - Failed to read current message #11

Closed amilich closed 4 weeks ago

amilich commented 1 month ago

Seeing this error line:

Failed to read current message, resetting size packets and waiting for more bytes.

I am able to log the buffer, but not sure how to decode it. I am running the code out of the box -

        const authSecret = generateFcmAuthSecret()
        const ecdh = createFcmECDH()
        const res = await registerToFCM({
            appID: ...
            ece: {
                authSecret: authSecret,
                publicKey: ecdh.getPublicKey(),
            },
            firebase: {
                apiKey: ...
                appID: ...
                projectID: ...
            },
            vapidKey: baseVapidKey,
        })
        client = new FcmClient({
            acg: {
                id: BigInt(res.acg.id),
                securityToken: BigInt(res.acg.securityToken),
            },
            heartbeat: {
                frequency: 500,
            },
        })
        client.on("message", message => {
            console.log("message")
            console.log(message)
        })

and this to send

const message: FcmApiMessageWithTarget = {
  token: <token created from above>,
  webpush: {
      data: {
        test: 'test'
      },
      notification: {
        title: 'test',
        body: 'testbody',
      },
      headers: {},
    },
    notification: {
      title: 'test',
      body: 'testbody',
    }
};

console.log('sending');
sendFcmMessage(GOOGLE_SERVICE_ACCOUNT, message)
  .catch((e) => {
    console.error(e);
  })
  .then((message) => {
    console.log(message);
  });

any idea why? just want to get the test example working

alchemicas commented 1 month ago

Hi @amilich , thanks for trying @aracna/fcm, you're forgetting the ece part inside the client, which is needed to decrypt the messages.

        const authSecret = generateFcmAuthSecret()
        const ecdh = createFcmECDH()
        const res = await registerToFCM({
            appID: ...
            ece: {
                authSecret: authSecret,
                publicKey: ecdh.getPublicKey(),
            },
            firebase: {
                apiKey: ...
                appID: ...
                projectID: ...
            },
            vapidKey: baseVapidKey,
        })
        client = new FcmClient({
            acg: {
                id: BigInt(res.acg.id),
                securityToken: BigInt(res.acg.securityToken),
            },
            ece: {
                authSecret: authSecret,
                privateKey: ecdh.getPrivateKey(),
            },
            heartbeat: {
                frequency: 500,
            },
        })
        client.on("message", message => {
            console.log("message")
            console.log(message)
        })

Let me know if adding that fixes your problem, thank you!

Reference in docs: FcmClient - Listen to messages

juangvega commented 1 month ago

Hello, @alchemicas !

I do have my setup as in https://github.com/queelag/fcm/blob/main/examples/node/src/register-and-listen.ts

And I always get this error when I receive messages. However, I'm able to receive and process the message, so I ignored this, as it does not seem to affect functionality.

image

alchemicas commented 1 month ago

Hi @juangvega , thanks for using @aracna/fcm! Could you please open another issue for this? I might just lower the level of that log since warn is a bit too much.

amilich commented 4 weeks ago

Hi @amilich , thanks for trying @aracna/fcm, you're forgetting the ece part inside the client, which is needed to decrypt the messages.

      const authSecret = generateFcmAuthSecret()
      const ecdh = createFcmECDH()
      const res = await registerToFCM({
          appID: ...
          ece: {
              authSecret: authSecret,
              publicKey: ecdh.getPublicKey(),
          },
          firebase: {
              apiKey: ...
              appID: ...
              projectID: ...
          },
          vapidKey: baseVapidKey,
      })
      client = new FcmClient({
          acg: {
              id: BigInt(res.acg.id),
              securityToken: BigInt(res.acg.securityToken),
          },
          ece: {
              authSecret: authSecret,
              privateKey: ecdh.getPrivateKey(),
          },
          heartbeat: {
              frequency: 500,
          },
      })
      client.on("message", message => {
          console.log("message")
          console.log(message)
      })

Let me know if adding that fixes your problem, thank you!

Reference in docs: FcmClient - Listen to messages

got it - this worked! thank you. I can close this issue.