Search code examples
typescriptfirebasefirebase-cloud-messagingfirebase-admin

how to import interface from Firebase Messaging from Admin SDK?


sorry I am newbie in Typescript and Node.

when sending FCM message in Typescript using Admin SDK, we can use this method

sendToDevice(registrationToken: string | string[], payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingDevicesResponse>;

that code taken from index.d.ts from firebase admin

when I want to use that method, I want to restrict the variable I create like this

const myPayload : MessagingPayload = {
    data: notification.toObject(),
    notification: {
        title: notification.title,
        body: notification.body,
        image: notification.imagePath,
    },
};

await admin.messaging().sendToDevice(tokens, myPayload, options);

but I need to import MessagingPayload first.

enter image description here

but now I am confused how to get / import that MessagingPayload interface

I try to import using this code below but it is error:

import { MessagingPayload } from "firebase-admin";

Solution

  • Each product's types are namespaced under the main export. You can write it like this:

    import * as admin from 'firebase-admin'
    const myPayload : admin.messaging.MessagingPayload = { ... }
    

    Or if you want to abbreviate it yourself, or change it completely:

    type MessagingPayload = admin.messaging.MessagingPayload
    const myPayload : MessagingPayload = { ... }