Firebase Push Notifications on Expo? (FCM)

In response to:

I’m working on an app using Expo, and I was planning on “pushing notifications” using FCM (Firebase). Do you support that?

for some technical reasons its not possible for us to support other push notification systems on the Expo Client besides the one we provide (which is free and simple)
docs here: https://docs.expo.io/versions/v14.0.0/guides/push-notifications.html
You could use Firebase push notifications by detaching/ejecting
and using ExpoKit
You’d have to deal with Xcode and Android Studio in that case but there is a path to do it
docs here: https://docs.expo.io/versions/v14.0.0/guides/exponentkit.html

I’m curious about the technical reasons for not supporting FCM; could you elaborate?

the main one is: we can’t expose the credentials to the expo client app in the app store because then anyone could send any message to any user of the client app. and we think its important to send push notifications in the client.

I think Expo is great as a pre-bundled set of native libraries that have been tested and validated together. Not having to worry about the native part of react-native makes the whole experience a lot better.

However, the end result should in my opinion still be a standalone build. There’s already an operational dependency on the Expo systems for the OTA updates and the dynamically loaded assets which I’m having a hard time turning off. Next to that, it’s impossible to send a push notification without using yet another Expo backend system. I understand the business case behind this strategy, but to me it’s a reason to eject as soon as the app has at least some users on it, and why Expo is probably not the right fit for large companies.

I will let my code using expo-server-sdk and firebase functions is necessary typscript

index.ts

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[Yourcode]"]) {
        // 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);
          }
        }
   
});
  "dependencies": {
    "@types/node-fetch": "^2.5.4",
    "expo-server-sdk": "^3.4.0",
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6",
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },

Dear Sir, Is this need paid account to send notifications using your code?

1 Like

No, is free,you need implement your own server, tray using firebase or other.
With this code you could implement your own server in nodejs

1 Like

Thanks for the reply,
I used your code. there is no error in the code. But it says in the function logs as “Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions”.