0
votes

I've an app based on react native with expo. For push notifications, i'm trying to use firebase. I've created functions in firebase and tried to send notification from firebase to my iphone. I don't see any issues in the code and verified firebase log too. Getting status "ok" message after this function triggered but still i didn't get notifications in my mobile. Anyone can help?

I'm in firebase paid plan. Same code is firing notification when i run directly from react native app.

const functions = require("firebase-functions");
const fetch = require("node-fetch");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

exports.sendPushNotification = functions.database
  .ref("orders/{id}")
  .onCreate((snapshot, context) => {
    var messages = [];
    const root = admin.database().ref("customers");
    console.log("customers : " + root.path);
    return root
      .once("value")
      .then((snapshot) => {
        snapshot.forEach((childSnapshot) => {
          //alert(expoToken);
          var expoToken = childSnapshot.val().expoPushToken;
          console.log("token : " + expoToken);
          if (expoToken) {
            messages.push({
              to: expoToken,
              title: "hello",
              body: "New Order",
            });
          }
        });
        return Promise.all(messages);
      })
      .then((messages) => {
        const res = fetch("https://exp.host/--/api/v2/push/send", {
          method: "POST",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
          },
          body: JSON.stringify(messages),
        });
        console.log(res);
        return res;
      });
  });
2

2 Answers

0
votes

Not sure what was the problem but after i added below line in headers tag, it started working fine.

"Accept-encoding": "gzip, deflate"

0
votes

I was not able to Comment, so i am writing here for database in firebase what you used actually 1. Cloud Firestore or 2. Realtime Database If you have used Firestore in this case Change code with

functions.firestore.document('Something\{id}').onCreate({snap, context}) => ...