https://supabase.com/ logo
Hi folks, I just want to share with you a little s...
# showcase
k
Hi folks, I just want to share with you a little story, i'm migrating a private app from firebase to supabase, from the beginning I know supabase doesn't have functions yet and thought the migration will take longer and by then the function (workflow) will come out however, the migration process didn't take long which is awesome of course, but I had a usecase using firebase where i had a function that listens to a db insert and triggers a push notification using fcm, due to security reasons of course, the user device table is private and only accessible by`creator_id = auth.uid()` therefore I decide to go the kinda impossible undocumented way, using procedure function with triggers & pgsql-http to try out if this is possible, after few hours of try and error, I was able to figure this out and make it work. here is some code that achieved that
Copy code
Trigger: 

create trigger notification_inserted
after
insert
  on notifications FOR EACH ROW EXECUTE PROCEDURE on_notification_created();
on_notification_created basically, construct some data and eventually call this method below::
Copy code
create
or replace function send_push(
  push_token text,
  title text,
  body text,
  ref_id uuid,
  creator_id uuid,
  n_type text
) returns void as $$
  SELECT
  status,
  content
  FROM
    http(
      (
        'POST',
        'https://fcm.googleapis.com/fcm/send',
        ARRAY [ http_header(
          'Authorization',
          'key=myawesomekey'
        ),
        http_header('Content-Type', 'application/json') ],
        'application/json',
        (
          select
            get_fcm_body(
              push_token,
              title,
              body,
              ref_id,
              creator_id,
              n_type
            )
        )
      ) :: http_request
    );
$$ language sql;
I believe this is worth mentioning as alot of who is switching from firebase will definitely want this until workflow is implemented. happy supabase'ing.