Update on 29 May 2018

Push notification via Firebase + Expo

const functions = require("firebase-functions");
// Import and initialize the Firebase Admin SDK.
const admin = require("firebase-admin");
const Expo = require("expo-server-sdk");
admin.initializeApp(functions.config().firebase);

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions

exports.saveTokens = functions.https.onRequest((request, response) => {
  let ok = true;
  let error = null;
  let token = request.body.token;
  if (token == "" || typeof token == "undefined") {
    ok = false;
    error = "Token is required";
  } else {
    let db = admin.database();
    let ref = db.ref().child("fcm_tokens");
    ref.set({ token });
  }
  response.send({
    ok,
    error
  });
});


exports.sendPushNotification = functions.https.onRequest(
  (request, response) => {
    let ok = true;
    let error = null;
    // send Notification
    let title = request.body.title;
    let body = request.body.body;

    admin.database()
      .ref("fcm_tokens/")
      .on("value", function(snapshot) {
        // Create a new Expo SDK client
        let expo = new Expo();

        // Create the messages that you want to send to clents
        let messages = [];
        snapshot.forEach(function(data) {
          let pushToken = data.val();
          // Each push token looks like ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]
          // console.log(pushToken)
          // 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;
          }

          // Construct a message (see https://docs.expo.io/versions/latest/guides/push-notifications.html)
          messages.push({
            to: pushToken,
            sound: "default",
            body,
            data: { withSome: "data" }
          });

          let chunks = expo.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 (let chunk of chunks) {
            try {
              let receipts = expo.sendPushNotificationsAsync(chunk)
              console.log(receipts);
            } catch (error) {
              console.error(error);
            }
          }


        }); // end snapshot


      }); // end for process

    // Return
    response.send({
      ok: ok,
      error: error
    });
  }
);
  • Step 3: Send Test Notification by ListApp manager plugin. Go to Push Notification under the ListApp Setting menu item.

Push notification via OneSignal

This feature require to detach the current Expo project to native project by using Expokit. Please note that this feature is kind of advance config when use the project as the native code, kindly keep patient during the setup. You can use this video guide for the installing: https://www.youtube.com/watch?v=6KRh7fusdIc&t=0s&index=7&list=PLcF-HiQy-jOLg0O6d9uWhOqGhvvLfx6is

Step 1 - Detach the project to native code:

Step 2 - Config OneSignal

results matching ""

    No results matching ""