简体   繁体   中英

firebase push notification with expo not working

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;
      });
  });

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

"Accept-encoding": "gzip, deflate"

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}) =>...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM