Created
August 27, 2016 17:24
-
-
Save ihadeed/e30db3c000561b9b6c5c4caff3dcd8d8 to your computer and use it in GitHub Desktop.
Push Notification Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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