Skip to content

Instantly share code, notes, and snippets.

@Florence-Njeri
Last active August 15, 2019 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Florence-Njeri/21425fa4e48a2e61956f6b4f4d357e28 to your computer and use it in GitHub Desktop.
Save Florence-Njeri/21425fa4e48a2e61956f6b4f4d357e28 to your computer and use it in GitHub Desktop.
const functions = require('firebase-functions');
// Firebase admin
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// The snapshot to the user's tokens.
// /* Trigger when a new document is created */
exports.createdMatchNotification = functions.firestore
// Add the document path to your collection mine is called matches
.document('match/{documentId}')
.onCreate((snap, context) => {
const user_id = context.params.user_id;
// Notifications builder
const payload = {
notification:{
title : 'Message from Cloud',
body : 'This is your body'
},
topic: 'newMatch'
};
return admin.messaging().send(payload);
});
/**
Its advisable to use shared preferences,you can have EG a switch where the user can
subscribe/unsubscribe to notifications. So find a way to implement this in your Shared Prefs
Dont forget to set up FCM in your app
*/
FirebaseMessaging.getInstance().subscribeToTopic("newMatch")
.addOnCompleteListener { task ->
var msg = "Subscription to get new match alerts successful"
if (!task.isSuccessful) {
msg = "Subscription to new match alerts failed"
}
Timber.d(msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment