-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirebase.txt
More file actions
90 lines (78 loc) · 3.12 KB
/
Firebase.txt
File metadata and controls
90 lines (78 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Realtime Database
private DatabaseReference mDatabase;
// Initialize the Database reference
mDatabase = FirebaseDatabase.getInstance().getReference();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
mDatabase.child("UsersUpdates").child(GlobalClass.UserUID).child("UpdateOrder").setValue(dateFormat.format(date));
// Check for updates in the order node
mDatabase.child("UsersUpdates").child("UpdateOrder").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Handle update if necessary
}
@Override
public void onCancelled(DatabaseError error) {
Log.w("DatabaseError", "Failed to read value.", error.toException());
}
});
// Remote Config
// Ensure your XML file is corrected as below:
<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
<entry>
<key>price</key>
<value>50 dolor</value>
</entry>
</defaultsMap>
// Code to run Remote Config
final FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
mFirebaseRemoteConfig.setDefaultsAsync(R.xml.price_tool); // Use setDefaultsAsync for latest versions
long exp = 0;
mFirebaseRemoteConfig.fetch(exp).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activate(); // activateFetched is deprecated
String price = mFirebaseRemoteConfig.getString("price");
Toast.makeText(getApplicationContext(), price, Toast.LENGTH_LONG).show();
}
}
});
// Crash Reporting
try {
int x = 10 / 0;
} catch (Exception ex) {
FirebaseCrashlytics.getInstance().log("Division by zero");
FirebaseCrashlytics.getInstance().recordException(ex);
}
// Storage - Upload Images
ImageView imageView = findViewById(R.id.imageView);
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://firbasedemo-6228f.appspot.com");
StorageReference mountainsRef = storageRef.child("images/mountains.jpg");
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
// Use the download URL
}
});
}
});