Update on 29 May 2018
Push notification via Firebase + Expo
- Login to google firebase to create your own account: https://console.firebase.google.com/
- Follow this video guide for detail setup:
Step 1: create an app with firebase by going to https://console.firebase.google.com
Step 2: select Functions menu on right hand side and create new function (ref: https://firebase.google.com/docs/functions/get-started\), then copy the following content function and paste to index.js file:
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:
On Mac OS run the detach script: yarn detach
One Window: npm install -g exp, then run exp detach
After the detach successful you can now open the project by Xcode or Android Studio.
Reference document: https://docs.expo.io/versions/latest/expokit/detach.html
Step 2 - Config OneSignal
- Setup your onesignal account - https://onesignal.com
Then follow this guide to config the the oneSignal library - https://documentation.onesignal.com/v5.0/docs/react-native-sdk-setup
Make sure the AppID from common/config.js is updated to match with your Onsignal AppID config
Reference document: https://beonews.inspireui.com/step-9-push-notification.html
This wordpress plugin is support to push new notification right after publish new post: https://wordpress.org/plugins/onesignal-free-web-push-notifications/