Last active
August 15, 2019 17:57
-
-
Save Florence-Njeri/21425fa4e48a2e61956f6b4f4d357e28 to your computer and use it in GitHub Desktop.
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
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