firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.63k stars 371 forks source link

Admin SDK v10 Messaging not working #1507

Closed paul-uz closed 2 years ago

paul-uz commented 2 years ago

I installed the latest version of the Firebase Admin SDK and setup a function to send a message to a topic

Code below (from a v9 SDK working function)


const firebaseCredentials = {
  type: PUSH_FIREBASE_AUTH.TYPE,
  project_id: PUSH_FIREBASE_AUTH.PROJECT_ID,
  private_key_id: PUSH_FIREBASE_AUTH.PRIVATE_KEY_ID,
  private_key: `-----BEGIN PRIVATE KEY-----\n${ PUSH_FIREBASE_AUTH.PRIVATE_KEY.replace(/\\n/g, '\n') }\n-----END PRIVATE KEY-----\n`,
  client_email: PUSH_FIREBASE_AUTH.CLIENT_EMAIL,
  client_id: PUSH_FIREBASE_AUTH.CLIENT_ID,
  auth_uri: PUSH_FIREBASE_AUTH.URI,
  token_uri: PUSH_FIREBASE_AUTH.TOKEN_URI,
  auth_provider_x509_cert_url: PUSH_FIREBASE_AUTH.PROVIDER_X509_CERT_URL,
  client_x509_cert_url: PUSH_FIREBASE_AUTH.CLIENT_X509_CERT_URL,
};

initializeApp({
      credential: credential.cert(firebaseCredentials),
    });

const message = {
        notification: {
          title: title,
          body: body,
        },
        apns: {
          payload: {
            aps: {
              sound: 'default',
            },
          },
        },
        topic: topic,
      };

return messaging().send(message);

I get a message ID returned, but I am not receiving the message on the test app. Firebase dashboard also reports the message being sent.

google-oss-bot commented 2 years ago

I found a few problems with this issue:

paul-uz commented 2 years ago

Update - I have reverted back to v9 for this test.

I then changed my code to replicate my other project:

const firebase = require('firebase-admin');
export class Firebase {
  constructor(credentials: any) {
    console.debug(credentials);
    firebase.initializeApp({
      credential: firebase.credential.cert(credentials),
    });
  }

  sendMessage = async (messageType: string, topicOrToken: string, title: string, body: string, data?: {}): Promise<string> => {
    let message;
    if (messageType === 'topic') {
      message = {
        notification: {
          title: title,
          body: body,
        },
        apns: {
          payload: {
            aps: {
              sound: 'default',
            },
          },
        },
        topic: topicOrToken,
      };
    }

    if (messageType === 'token') {
      message = {
        notification: {
          title: title,
          body: body,
        },
        apns: {
          payload: {
            aps: {
              sound: 'default',
            },
          },
        },
        token: topicOrToken,
      };
    }

    console.debug({message});

    return firebase.messaging().send(message);
  }

  sendTopicMessage = async (topic: string, title: string, body: string, data?: {}) => {
    return this.sendMessage('topic', topic, title, body, data)
  }

  sendTokenMessage = async (token: string, title: string, body: string, data?: {}) => {
    return this.sendMessage('token', token, title, body, data)
  }
}

This now works.

However, If I change the require() to an import, and the TypeScript compilation errors kick in about the data types. So I assign the correct data type notations for the credentials and message.

But now, with a data type of ServiceAccount for the credentials, the object I was using successfully, is no longer compatible. None of the declared key names match, yet before assigning the data type, it worked.

Type '{ type: string | undefined; project_id: string | undefined; private_key_id: string | undefined; private_key: string; client_email: string | undefined; client_id: string | undefined; auth_uri: string | undefined; token_uri: string | undefined; auth_provider_x509_cert_url: string | undefined; client_x509_cert_url: str...' has no properties in common with type 'ServiceAccount'.ts(2559)

What is going on?

paul-uz commented 2 years ago

So the TypeScript issue is this https://github.com/firebase/firebase-admin-node/issues/522