Skip to content

Instantly share code, notes, and snippets.

@e-lin
e-lin / upload.java
Created June 25, 2019 10:13
Uploading to Firebase Cloud Storage
private fun uploadFile(f: File) {
val sdf = SimpleDateFormat("yyyyMMddHHmmss", Locale.US)
val storagePath = "/pictures/${sdf.format(Date())}.jpg"
val ref = FirebaseStorage.getInstance().getReference(storagePath)
ref.putFile(Uri.fromFile(f))
.addOnSuccessListener(this) {
Log.i(TAG, "picture uploaded")
}
.addOnFailureListener(this) { e ->
Log.i(TAG, "upload failed", e)
@e-lin
e-lin / pub1.java
Last active June 26, 2019 03:08
Publish a Nearby Message 1 - configuration
val strategy = Strategy.Builder()
.setDiscoveryMode(Strategy.DISCOVERY_MODE_BROADCAST)
.setTtlSeconds(Strategy.TTL_SECONDS_MAX)
.build()
val publishOpts = PublishOptions.Builder()
.setStrategy(strategy)
.setCallback(object : PublishCallback()) {
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)
}
@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 / 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 / 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 / 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 / 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 / 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 / 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)