Skip to content

Instantly share code, notes, and snippets.

@ihadeed
Created August 27, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihadeed/e30db3c000561b9b6c5c4caff3dcd8d8 to your computer and use it in GitHub Desktop.
Save ihadeed/e30db3c000561b9b6c5c4caff3dcd8d8 to your computer and use it in GitHub Desktop.
Push Notification Example
import gcm = require('node-gcm');
let sender = new gcm.Sender('YOUR_GCM_SENDER_ID'); // this is basically your API key
function sendPushNotification(message, gcmIds){
sender.send(message, {registerationTokens: gcm_ids}, (err, response) => {
if(err) console.log(err);
console.log(response);
});
}
function notifyAboutLike(postId, userId){
// first lets get the post from mongoose
Post.findById(postId, (err, post) => {
let message = new gcm.Message({
notification: {
title: 'New post like',
body: 'Someone just liked your post ' + post.postTitle
},
data: {
postId: postId // you can read this value in the app, and let your app open that post automatically
}
});
// lets get that user's GCM_ID
PushNotificationRegisterations.find({
user: userId
}, (err, doc) => {
sendPushNotification(message, [doc.gcm_id]);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment