Skip to content

Instantly share code, notes, and snippets.

@e-lin
e-lin / FirestoreTrigger2.java
Created June 26, 2019 08:28
Firestore trigger 2 - send answer to Android Thing
const payload = {
data: {
disposition: pic.answer.disposition.toString(),
pic_id: picId
}
}
try {
const response = await fcm.sendToTopic('answers',payload)
@e-lin
e-lin / FirestoreTrigger1.java
Created June 26, 2019 08:24
Firestore trigger 1 - send answer to Android Thing
export const onAnswer = functions.firestore.document('/pic/{picId}').onUpdate(_onAnswer)
async function _onAnswer(change: functions.Change<DocumentSnapshot>): Promise<any> {
const picId = change.before.id "//20180624120000"
const previous = change.before.data() as Pic
const pic = change.after.data() as Pic
// Only interested in pics that have a new answer
if (previous.answer || !pic.answer) {
console.log("This is not the update you are looking for.")
return Promise.resolve()
@e-lin
e-lin / dispose.java
Created June 26, 2019 08:11
Update catfood bowl disposition in Firestore
val disposition = button_click_true_or_false
picReference.update(
"answer.uid", uid,
"answer.disposition", disposition)
.addOnCompleteListener(this) {
Log.d(TAG, "Answer written to database")
finish()
}
.addOnFailureListener(this, {e ->
Log.d(TAG, "Answer not written to database", e)
@e-lin
e-lin / glide.java
Created June 26, 2019 08:03
Glide Module - Cloud Storage for Firebase plugin
@GlideModule
class MyAppGlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
// Register FirebaseImageLoader from FirebaseUI to handle StorageReference
registry.append(StorageReference::class.java, InputStream::class.java,
FirebaseImageLoader.Factory())
}
}
@e-lin
e-lin / fetchPic.java
Created June 26, 2019 07:45
Fetch pic data from Firebase
private fun poplulateViews(picId: String) {
picReference = FirebaseFirestore.getInstance().collection("pics").document(picId)
picReference.get()
.addOnSuccessListener(this) { snap ->
if (snap.exists()) {
val ring = snap.toObject(Pic::class.java)
val ref = FirebaseStorage.getInstance().getReference(pic.imagePath!!)
Glide.with(this@AnswerPicActivity).load(ref).into(ivGuest)
}
}
@e-lin
e-lin / trigger2.java
Created June 26, 2019 04:09
Storage upload trigger 2 - send notification to app
// sned a notification to the app
const payload = {
notification: {
title: 'Hellooo',
body: 'Is my kitty\'s bowl empty?',
click_action: 'com.hyperaware.catfeeder.ANSWER_PIC'
},
data: {
pic_id: id
}
@e-lin
e-lin / trigger 1.java
Created June 26, 2019 04:02
Storage upload trigger 1 - add document to Firestore
export const onPic = functions.storage.object().onFinalize(_onPic)
async function _onPic(object: functions.storage.ObjectMetadata): Promise<any> {
const path = object.name // "pictures/20180624120000.jpg"
const id = basename(path, '.jpg') // "20180624120000"
try {
// add a document to Firestore with the details of this pic
const pic: Pic = {
id: id,
date: new Date(),
@e-lin
e-lin / sub2.java
Created June 26, 2019 03:07
Subscribe to a Nearby Message 2 - subscribe
val client = Nearby.getMessagesClient(this)
private val messageListener = object : MessageListener() {
override fun onFound(message: Message) {
// message.content contains payload
}
override fun onLost(message: Message) {}
}
client.subscribe(messageListener, subscribeOpts)
@e-lin
e-lin / sub1.java
Created June 26, 2019 03:02
Subscribe to a Nearby Message 1 - configuration
val strategy = Strategy.Builder()
.setDiscoveryMode(Strategy.DISCOVERY_MODE_SCAN)
.setTtlSeconds(Strategy.TTL_SECONDS_MAX)
.build()
val subscribeOpts = SubscribeOpts.Builder()
.setStrategy(strategy)
.setCallback(object : setCallback() {
override fun onExpired() {
Log.d(TAG, "onExpired")
@e-lin
e-lin / pub2.java
Created June 26, 2019 02:56
Publish a Nearby Message 2 - publish
val client = Nearby.getMessagesClient(this)
val message = Message("Hi, Firebase Thing.")
client.publish(message, publishOpts)
.addOnSuccessListener(this) {
Log.e(TAG, "publish scucess")
}
.addOnFailureListener(this) { e ->
Log.e(TAG, "publish failed", e)
}