Supabase sending push notification via firebase

I am creating an edge function in supabase to send a push notification to a user’s device.

Here is my edge function

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { initializeApp, cert } from "https://esm.sh/firebase-admin/app"
import { getMessaging } from "https://esm.sh/firebase-admin/messaging"

var serviceAccount = cert({
  projectId: "**redacted**",
  privateKey: "**redacted**",
  clientEmail: "**redacted**",
})

const app =initializeApp({
  credential: serviceAccount
});

const TEST_FCM_TOKEN = "**redacted**"

export interface momentNotification {
  data: {
    title: string,
    message: string,
  }
}

serve(async (req) => {
  const { name } = await req.json()
  const data = {
    message: `Hello ${name}!`,
  }


  const defaultMessaging = getMessaging(app);

  defaultMessaging.sendToDevice(TEST_FCM_TOKEN, {
    notification: {
      title: 'title',
      body: 'description'
    },
  }).then(function(response) {
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    console.log("Error sending message:", error);
  });

  return new Response(
    JSON.stringify(data),
    { headers: { "Content-Type": "application/json" } },
  )
})

There is additional parts of the boiler plate code that does the name stuff which can be seen when the function is curled. I am running it locally using npx supabase functions serve.

There is a good connection formed between the edge function and firebase as I have got past error of bad keys etc.

Here is my error

Error sending message: TypeError: Right-hand side of 'instanceof' is not an object
    at Object.de.exports [as sign] (https://esm.sh/v132/[email protected]/esnext/jsonwebtoken.mjs:16:10119)
    at x.createAuthJwt_ (https://esm.sh/v132/[email protected]/esnext/app.js:20:8224)
    at x.getAccessToken (https://esm.sh/v132/[email protected]/esnext/app.js:20:7722)
    at se.refreshToken (https://esm.sh/v132/[email protected]/esnext/app.js:20:24514)
    at se.getToken (https://esm.sh/v132/[email protected]/esnext/app.js:20:24361)
    at ze.getToken (https://esm.sh/v132/[email protected]/esnext/messaging.js:20:4653)
    at ze.send (https://esm.sh/v132/[email protected]/esnext/messaging.js:20:4378)
    at ot.invokeRequestHandler (https://esm.sh/v132/[email protected]/esnext/messaging.js:33:643)
    at https://esm.sh/v132/[email protected]/esnext/messaging.js:33:7479

Leave a Comment