Skip to content

Instantly share code, notes, and snippets.

View hiranya911's full-sized avatar

Hiranya Jayathilaka hiranya911

View GitHub Profile
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
package main
import (
"context"
"log"
firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
)
const admin = require('firebase-admin');
const HttpsProxyAgent = require('https-proxy-agent');
const agent = new HttpsProxyAgent('http://localhost:3128');
const app = admin.initializeApp({
credential: admin.credential.applicationDefault(agent),
httpAgent: agent
});
(async () => {
const db = admin.firestore();
import firebase_admin
from firebase_admin import messaging
from firebase_admin import firestore
firebase_admin.initialize_app()
client = firestore.client()
snapshot = client.collection('users').document('alice').get()
data = snapshot.to_dict()
msg = messaging.Message(
private val db = FirebaseFirestore.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupPriceListeners()
}
private fun setupPriceListeners() {
db.collection("prices")
.addSnapshotListener(EventListener<QuerySnapshot> { value, e ->
private suspend fun savePref(currency: String, min: Double, max: Double) {
val instanceId = FirebaseInstanceId.getInstance().instanceId.await()
val data = mapOf(
"${currency}_min" to min,
"${currency}_max" to max,
"token" to instanceId.token
)
db.collection("prefs").document(instanceId.id)
.set(data, SetOptions.merge()).await()
}
class CryptoAlertMessagingService: FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage?) {
message?.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
val mainHandler = Handler(mainLooper)
mainHandler.post {
Toast.makeText(applicationContext, "${it.body}", Toast.LENGTH_SHORT).show()
}
}
import com.google.android.gms.tasks.Task
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
suspend fun <T> Task<T>.await() =
suspendCancellableCoroutine<T> {
this.addOnSuccessListener(it::resume)
this.addOnFailureListener(it::resumeWithException)
this.addOnCanceledListener { it.cancel() }
}
// Prices is a map of crypto currency prices.
type Prices map[string]float64
// SavePrices saves the given crypto currency prices to the database.
func (client *FirebaseClient) SavePrices(ctx context.Context, prices Prices) error {
collection := client.db.Collection("prices")
batch := client.db.Batch()
for curr, price := range prices {
batch.Set(collection.Doc(curr), map[string]interface{}{"value": price}, firestore.MergeAll)
}
cron:
- description: "cryptocurrency price updater"
url: /fetch
schedule: every 30 minutes