Closed paul-uz closed 2 years ago
I found a few problems with this issue:
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?
So the TypeScript issue is this https://github.com/firebase/firebase-admin-node/issues/522
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)
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.