Skip to content

Instantly share code, notes, and snippets.

@vibgy
Created August 15, 2015 00:20
Show Gist options
  • Save vibgy/6f17abf0a65889697d3f to your computer and use it in GitHub Desktop.
Save vibgy/6f17abf0a65889697d3f to your computer and use it in GitHub Desktop.
Firebase connector to send Push notifications using Parse push notifications
var FirebaseTokenGenerator = require("firebase-token-generator");
var Firebase = require("firebase");
var async = require("async");
var steps = [];
var tokenGenerator = new FirebaseTokenGenerator("sdfsdfsdfsdfsdf");
var token = tokenGenerator.createToken({ uid: "uniqueId1", some: "arbitrary", data: "here" });
var ref = new Firebase("https://sdfsdfsdf.firebaseio.com/messages/xyz");
var Parse = require('node-parse-api').Parse;
var APP_ID = "sdfsdfsdfsdf";
var MASTER_KEY = "sdfsdfsdfsdf";
var app = new Parse(APP_ID, MASTER_KEY);
steps.push(function init(done) {
ref.authWithCustomToken(token, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Login Succeeded!", authData);
}
done(error);
});
});
steps.push(function one(done) {
ref.once('value', function(data) {
console.log(data);
done();
})
});
var ignorePush = true;
steps.push(function(done) {
//ref.orderByChild("date").limitToLast(1).on("child_added", function(snapshot) {
ref.limitToLast(1).on("child_added", function(snapshot) {
console.log(snapshot.val());
if (!ignorePush) {
sendPush(snapshot.val());
} else {
ignorePush = false;
}
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
done();
});
async.series(steps, function(err) {
console.log("Cool");
});
function sendPush(data) {
var msg = "test"
if (data) {
msg = (data.author || "") + ":" + (data.message || "").substring(0,20);
}
var notification = {
channels: [''],
data: {
alert: msg
}
};
app.sendPush(notification, function(err, resp){
if (err) {
console.log(err);
} else {
console.log(resp);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment