Expo push notification with firebase cloud function

Hello,

I need to send push notification with expo and firebase clouud function because of I m using firebase database.
it will triggered and send push when there is any update on firebase database. is there any solution for this issue???

firebase cloud function is not support (async functions) so ı could not use the “await …” type for exponent-server-sdk. how can I handle it?

Thank you…

1 Like

is there anybody to answer and be hero:))

2 Likes

you could use OneSignal and call it from firebase function

btw cloud functions was down the past day. It’s back up now

How can I use onesignal with expoPushToken? or should I use only onesignal??

Sorry i’m using OneSignal because we ejected/detached so we couldn’t use expo push tokens.
I never implemented expo push token…

Don’t think you can do that.

I had this same question last week and decided to detach from Expo for the sake of FCM.

1 Like

Hi,

did you handle it? is there any problem on detaching from expo?

@suatozkan While firebase cloud messaging cannot be used in the regular expo configuration without detaching, I believe that firebase cloud functions are possible. Would using await + fetch work, perhaps as described in this medium article here: Our extensive experience with Cloud Functions for Firebase | by Tomáš Bezouška | Step Up Labs | Medium ?

There have also been accounts of Expo users getting firebase cloud functions working in this thread here: Expo and uploading image Blobs? - #14 by tscans

1 Like

I use ZetPush https://zetpush.com/ for web notifications. Is very easy to use.

I’m interested in a solution to this as well. It seems like I should be able to just write a cloud function that uses the ExpoClient described in their node documentation however when I try to do that, it doesn’t seem to work. Perhaps it’s because I’m not installing the expo-client-sdk correctly? I’m still new to firebase so if that’s the case, I’ll need another solution to handle push notifications.

Hey @fourcolors,

Are you on a paid plan for Firebase? The free plan only allows for outbound network requests to Google-owned services. In order to use the expo node server, you’ll have to be on a paid plan.

Cheers,

Adam

So it looks like my issue was I was importing is incorrectly. I was following the import syntax for the docs but that didn’t work on firebase, I had to do something like import * as Expo from 'expo-server-sdk'

3 Likes

Legend! I was having the same issue. I think this will help a lot of people.

1 Like

I’ve got a tutorial on using cloud functions with expo push notifications. : React Native + Firebase #6 Send Push Notifications ( Expo + Firebase Cloud Functions) - YouTube
Hope it helps!

1 Like

I used your tutorial and everything works. but i keep getting this error in the cloud function. its firing off and im getting the data etc. but its not doing the route correctly.

Is this perhaps because you haven’t switched to the Blaze plan to support outbound requests?

1 Like
import * as functions from 'firebase-functions';

import * as admin from 'firebase-admin';

import { Expo, ExpoPushMessage } from 'expo-server-sdk';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
admin.initializeApp()
//let expo = new Expo();
export const prueba = functions.firestore.document('prueba/TY6SCYQwSEk1M7sPzWK4').onUpdate(()=>{
    //const allTokens = [];
    const messages: ExpoPushMessage[] = [];
    const exp  = new Expo()

    for (const pushToken of ["ExponentPushToken[Ixxxxxxxxxxxxxxxxxxxxxxn]"]) {
        // Each push token looks like ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]
      
        // Check that all your push tokens appear to be valid Expo push tokens
        if (!Expo.isExpoPushToken(pushToken)) {
          console.error(`Push token ${pushToken} is not a valid Expo push token`);
          continue;
        }else{
            console.log("funciono el atoken");
        }
      
        // Construct a message (see https://docs.expo.io/versions/latest/guides/push-notifications)
        messages.push({
          to: pushToken,
          sound: 'default',
          body: 'Dalvin Notificacion',
          data: { withSome: 'data' },
        })
      }
 
      const chunks = exp.chunkPushNotifications(messages);
    
        // Send the chunks to the Expo push notification service. There are
        // different strategies you could use. A simple one is to send one chunk at a
        // time, which nicely spreads the load out over time:
        for (const chunk of chunks) {
          try {
            const ticketChunk =  exp.sendPushNotificationsAsync(chunk);
            console.log(ticketChunk);
       
            // NOTE: If a ticket contains an error code in ticket.details.error, you
            // must handle it appropriately. The error codes are listed in the Expo
            // documentation:
            // https://docs.expo.io/versions/latest/guides/push-notifications#response-format
          } catch (error) {
            console.error(error);
          }
        }
   
});
1 Like

I used firebase functions and expo to push notifications
But when I sign up by phone to firebase, It has error like: auth/invalid-app-credential
Does anyone occor this problem?