queelag / fcm

MIT License
6 stars 2 forks source link

Incomplete message data when sending notification from OneSignal #6

Open joshua-redmond opened 2 months ago

joshua-redmond commented 2 months ago

Hi!

How do I get the message's heading and body once it's received?

I'm sending messages using OneSignal (read more about my setup here).

This is what I get from FcmClient's message event:

DataMessageStanza {
  app_data: [
    AppData { key: 'google.source', value: 'webpush' },
    AppData {
      key: 'encryption',
      value: 'salt=bQN_w1wSjQYoW1_-2z5Ueg=='
    },
    AppData {
      key: 'subtype',
      value: '1:847954429589:android:fb3995c643624ddf567d9'
    },
    AppData {
      key: 'crypto-key',
      value: 'dh=BPp8uF-kAoVzyllad_OsGx9kqCC-d6oH9QHWuQx2uQVsxdO3bDq1qsW5kOVsazm1Vw7NJdk6RkNwnPf6WWAZk-E='
    }
  ],
  id: 'D0F8F476',
  from: 'BDOU99-h67HcA6JeFXHbSNMu7e2yNNu3RzoMj8TM4W88jITfq7ZmPvIM1Iv-4_l2LxQcYwhqby2xGpWwzjfAnG4',
  category: 'org.chromium.linux',
  persistent_id: '0:1712828183130008%7031b2e6f9fd7ecd',
  ttl: 2419200,
  sent: Long { low: -863767978, high: 398, unsigned: false },
  raw_data: <Buffer 2d b2 13 5e 60 41 04 da 77 42 2b 7c 66 a4 4a 1c f5 0a 76 98 7d 8d f4 54 93 df fb 9f f3 ce 51 e2 d6 c0 9e f1 3e b8 0f 84 c3 59 a2 45 40 65 1d c0 b7 4a ... 65 more bytes>
}

This is what I get from the message-data event:

{
  from: '------------', // Dashed out for security.
  priority: 'normal',
  fcmMessageId: 'b78b9742-7912-4206-8765-06b150e51034'
}

How do I get the message's heading and body from this result?

Here's my code, based on the node example kindly provided in the repo:

import {
    FcmClient,
    createFcmECDH,
    generateFcmAuthSecret,
    registerToFCM
} from '@aracna/fcm'

const APP_ID = '1:847954429589:android:fb3995c643624ddf567d9'
const FIREBASE_API_KEY = '---------------------------------------' // Dashed out for security.
const FIREBASE_APP_ID = '1:847954429589:android:fb3995c643624ddf567d9'
const FIREBASE_PROJECT_ID = '-----------' // Dashed out for security.
const VAPID_KEY = '---------------------------------------------------------------------------------------' // Dashed out for security.

let authSecret, ecdh, registration;

authSecret = generateFcmAuthSecret()
ecdh = createFcmECDH()

registration = await registerToFCM({
    appID: APP_ID,
    ece: {
        authSecret: authSecret,
        publicKey: ecdh.getPublicKey()
    },
    firebase: {
        apiKey: FIREBASE_API_KEY,
        appID: FIREBASE_APP_ID,
        projectID: FIREBASE_PROJECT_ID
    },
    vapidKey: VAPID_KEY
})
console.log(registration)

var fcm = {
    ece: {
    authSecret: authSecret,
    privateKey: ecdh.getPrivateKey(),
    publicKey: ecdh.getPublicKey()
    },
    ...registration
};

var client = new FcmClient({
    acg: {
        id: fcm.acg.id,
        securityToken: fcm.acg.securityToken
    },
    ece: {
        authSecret: fcm.ece.authSecret,
        privateKey: fcm.ece.privateKey
    }
});

client.on('login', (login) => {
    console.log(login)
})

client.on('message', (message) => {
    console.log('message', message);
})

client.on('message-data', (message) => {
    console.log('message-data', message)
})

client.connect();

I also tried using the node example in it's entirety, though ran into this error when running npm run send-message:

/home/mike/Downloads/test/fcm/examples/node/node_modules/gtoken/build/src/index.js:170
        throw new Error('No key or keyFile set.');
              ^

Error: No key or keyFile set.
    at GoogleToken._GoogleToken_getTokenAsyncInner (/home/mike/Downloads/test/fcm/examples/node/node_modules/gtoken/build/src/index.js:170:15)
    at GoogleToken._GoogleToken_getTokenAsync (/home/mike/Downloads/test/fcm/examples/node/node_modules/gtoken/build/src/index.js:160:173)
    at GoogleToken.getToken (/home/mike/Downloads/test/fcm/examples/node/node_modules/gtoken/build/src/index.js:110:102)
    at JWT.refreshTokenNoCache (/home/mike/Downloads/test/fcm/examples/node/node_modules/google-auth-library/build/src/auth/jwtclient.js:173:36)
    at JWT.refreshToken (/home/mike/Downloads/test/fcm/examples/node/node_modules/google-auth-library/build/src/auth/oauth2client.js:158:25)
    at JWT.authorizeAsync (/home/mike/Downloads/test/fcm/examples/node/node_modules/google-auth-library/build/src/auth/jwtclient.js:154:35)
    at JWT.authorize (/home/mike/Downloads/test/fcm/examples/node/node_modules/google-auth-library/build/src/auth/jwtclient.js:150:25)
    at c (file:///home/mike/Downloads/test/fcm/examples/node/node_modules/@aracna/fcm/utils/google-auth-utils.js:1:181)
    at h (file:///home/mike/Downloads/test/fcm/examples/node/node_modules/@aracna/fcm/requests/fcm-requests.js:1:314)
    at Module.g (file:///home/mike/Downloads/test/fcm/examples/node/node_modules/@aracna/fcm/functions/send-fcm-message.js:1:92)

The npm start gave me a token as expected, so who knows what went wrong.


BTW, thanks for all your hard work creating this package! I really tried to wrap my head around FCM's new HTTP v1 API but gave up haha, I'm glad you understand it :laughing:

alchemicas commented 2 months ago

Hi @joshua-redmond , first of all thank you for trying out my library!

  1. Regarding the missing data from the "message-data" event, mh that is weird for sure, I might have to replicate it myself by creating a onesignal account and sending a message from there
  2. As for the send-message script not working inside the example, you need a firebase admin config, which must be either provided base64 encoded through the .env file or pasted as it is inside the source code

You can get the firebase admin config from this page: https://console.firebase.google.com/u/0/project/your_project_name/settings/serviceaccounts/adminsdk

I'll post an update once I try onesignal + aracna/fcm, lmk if you manage to get the send message working in the example :D

joshua-redmond commented 1 month ago

@alchemicas I can confirm the send-message script works as expected after adding the service account JSON. The notification data is received.

That means it's only a problem when sending notifications using OneSignal.

alchemicas commented 1 month ago

Thank you @joshua-redmond , will test it against OneSignal this week!

alchemicas commented 1 month ago

Hello @joshua-redmond , I gave it a try today, but I honestly have no idea how to make it work, no matter what I put inside the notification I create on OneSignal, the raw_data that the FCM client receives is always the same bytes.

I tried to investigate and look at OneSignal's SDKs code, but I couldn't make much sense of it in the time I was looking. My theory is that they just don't put the payload of the notification inside the FCM send API, and they use their own API to deal with the notification data. But it's still just a theory since I wasn't able to confirm it by looking at their code.

So by using OneSignal you're basically to square one since they do not have a Node.js client for receiving notification, but just for creating them.

If you can find an answer or a hint I'd be eager to help and make this work somehow, but if we have to call OneSignal APIs then it's going to be out of scope for this library.

I won't investigate any further, but you or anyone else who's interested in this is welcome to help!

joshua-redmond commented 1 month ago

Hi, good to hear from you again.

That's... unfortunate :disappointed:

I can't think of a way around it either.

Thanks for your time investigating this issue :+1: