Java Code Examples for com.google.firebase.database.DatabaseReference#getKey()
The following examples show how to use
com.google.firebase.database.DatabaseReference#getKey() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegrationTestUtils.java From firebase-admin-java with Apache License 2.0 | 5 votes |
public static List<DatabaseReference> getRandomNode(FirebaseApp app, int count) { FirebaseDatabase database = FirebaseDatabase.getInstance(app); ImmutableList.Builder<DatabaseReference> builder = ImmutableList.builder(); String name = null; for (int i = 0; i < count; i++) { if (name == null) { DatabaseReference ref = database.getReference().push(); builder.add(ref); name = ref.getKey(); } else { builder.add(database.getReference().child(name)); } } return builder.build(); }
Example 2
Source File: GroupsSyncronizer.java From chat21-android-sdk with GNU Affero General Public License v3.0 | 5 votes |
public void createChatGroup(final String chatGroupName, Map<String, Integer> members, final ChatGroupCreatedListener chatGroupCreatedListener) { DatabaseReference newGroupReference = this.appGroupsNode.push(); String newGroupId = newGroupReference.getKey(); final ChatGroup chatGroup = createGroupForFirebase(chatGroupName, members); chatGroup.setGroupId(newGroupId); newGroupReference.setValue(chatGroup, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { if (databaseError == null) { Log.d(DEBUG_GROUPS, "GroupsSyncronizer.createChatGroup:" + " databaseReference == " + databaseReference.toString()); saveOrUpdateGroupInMemory(chatGroup); if (chatGroupCreatedListener != null) { chatGroupCreatedListener.onChatGroupCreated(chatGroup, null); } } else { Log.e(DEBUG_GROUPS, "GroupsSyncronizer.createChatGroup:" + " cannot create chatGroup " + databaseError.toException()); if (chatGroupCreatedListener != null) { chatGroupCreatedListener.onChatGroupCreated(null, new ChatRuntimeException(databaseError.toException())); } } } }); }
Example 3
Source File: ChatActivity.java From ChatApp with Apache License 2.0 | 4 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1 && resultCode == RESULT_OK) { Uri url = data.getData(); DatabaseReference messageRef = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUserId).child(otherUserId).push(); final String messageId = messageRef.getKey(); DatabaseReference notificationRef = FirebaseDatabase.getInstance().getReference().child("Notifications").child(otherUserId).push(); final String notificationId = notificationRef.getKey(); StorageReference file = FirebaseStorage.getInstance().getReference().child("message_images").child(messageId + ".jpg"); file.putFile(url).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() { @Override public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) { if(task.isSuccessful()) { String imageUrl = task.getResult().getDownloadUrl().toString(); Map messageMap = new HashMap(); messageMap.put("message", imageUrl); messageMap.put("type", "image"); messageMap.put("from", currentUserId); messageMap.put("to", otherUserId); messageMap.put("timestamp", ServerValue.TIMESTAMP); HashMap<String, String> notificationData = new HashMap<>(); notificationData.put("from", currentUserId); notificationData.put("type", "message"); Map userMap = new HashMap(); userMap.put("Messages/" + currentUserId + "/" + otherUserId + "/" + messageId, messageMap); userMap.put("Messages/" + otherUserId + "/" + currentUserId + "/" + messageId, messageMap); userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/message", "You have sent a picture."); userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/timestamp", ServerValue.TIMESTAMP); userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/seen", ServerValue.TIMESTAMP); userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/message", "Has send you a picture."); userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/timestamp", ServerValue.TIMESTAMP); userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/seen", 0); userMap.put("Notifications/" + otherUserId + "/" + notificationId, notificationData); FirebaseDatabase.getInstance().getReference().updateChildren(userMap, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { sendButton.setEnabled(true); if(databaseError != null) { Log.d(TAG, "sendMessage(): updateChildren failed: " + databaseError.getMessage()); } } }); } } }); } }
Example 4
Source File: ChatActivity.java From ChatApp with Apache License 2.0 | 4 votes |
private void sendMessage() { sendButton.setEnabled(false); String message = messageEditText.getText().toString(); if(message.length() == 0) { Toast.makeText(getApplicationContext(), "Message cannot be empty", Toast.LENGTH_SHORT).show(); sendButton.setEnabled(true); } else { messageEditText.setText(""); // Pushing message/notification so we can get keyIds DatabaseReference userMessage = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUserId).child(otherUserId).push(); String pushId = userMessage.getKey(); DatabaseReference notificationRef = FirebaseDatabase.getInstance().getReference().child("Notifications").child(otherUserId).push(); String notificationId = notificationRef.getKey(); // "Packing" message Map messageMap = new HashMap(); messageMap.put("message", message); messageMap.put("type", "text"); messageMap.put("from", currentUserId); messageMap.put("to", otherUserId); messageMap.put("timestamp", ServerValue.TIMESTAMP); HashMap<String, String> notificationData = new HashMap<>(); notificationData.put("from", currentUserId); notificationData.put("type", "message"); Map userMap = new HashMap(); userMap.put("Messages/" + currentUserId + "/" + otherUserId + "/" + pushId, messageMap); userMap.put("Messages/" + otherUserId + "/" + currentUserId + "/" + pushId, messageMap); userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/message", message); userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/timestamp", ServerValue.TIMESTAMP); userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/seen", ServerValue.TIMESTAMP); userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/message", message); userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/timestamp", ServerValue.TIMESTAMP); userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/seen", 0); userMap.put("Notifications/" + otherUserId + "/" + notificationId, notificationData); // Updating database with the new data including message, chat and notification FirebaseDatabase.getInstance().getReference().updateChildren(userMap, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { sendButton.setEnabled(true); if(databaseError != null) { Log.d(TAG, "sendMessage(): updateChildren failed: " + databaseError.getMessage()); } } }); } }
Example 5
Source File: ConversationMessagesHandler.java From chat21-android-sdk with GNU Affero General Public License v3.0 | 4 votes |
public void sendMessage(String type, String text, String channelType, final Map<String, Object> metadata, final SendMessageListener sendMessageListener) { Log.v(TAG, "sendMessage called"); // the message to send final Message message = new Message(); message.setSender(currentUser.getId()); message.setSenderFullname(currentUser.getFullName()); message.setRecipient(this.recipient.getId()); message.setRecipientFullname(this.recipient.getFullName()); message.setStatus(Message.STATUS_SENDING); message.setText(text); message.setType(type); message.setChannelType(channelType); // message.setStatus(Message.STATUS_SENDING); //problema se il client ha orologio sballato (avanti nel tempo rispetto a orario server) message.setTimestamp(new Date().getTime()); //oltre a questo caso devi sull'on update aggiornare il messaggio quando ti arriva prendendo il timestamp del server //TODO //message.setTimestamp(ServerValue.TIMESTAMP); message.setMetadata(metadata); Log.d(TAG, "sendMessage.message: " + message.toString()); // generate a message id DatabaseReference newMessageReference = conversationMessagesNode.push(); String messageId = newMessageReference.getKey(); Log.d(TAG, "Generated messagedId with value : " + messageId); message.setId(messageId); // assign an id to the message saveOrUpdateMessageInMemory(message); // conversationMessagesNode // .push() // Message messageForSending = createMessageForFirebase(message); Map messageForSending = message.asFirebaseMap(); Log.d(TAG, "sendMessage.messageForSending: " + messageForSending.toString()); newMessageReference .setValue(messageForSending, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { Log.v(TAG, "sendMessage.onComplete"); if (databaseError != null) { String errorMessage = "sendMessage.onComplete Message not sent. " + databaseError.getMessage(); Log.e(TAG, errorMessage); FirebaseCrash.report(new Exception(errorMessage)); if (sendMessageListener != null) { sendMessageListener.onMessageSentComplete(null, new ChatRuntimeException(databaseError.toException())); } } else { Log.d(TAG, "message sent with success to : " + databaseReference.toString()); //the cloud code set status to 100 automaticaly //message.setStatus(Message.STATUS_SENT); saveOrUpdateMessageInMemory(message); // databaseReference.child("status").setValue(Message.STATUS_RECEIVED); // databaseReference.child("customAttributes").setValue(customAttributes); //TODO lookup and return the message from the firebase server to retrieve all the fields (timestamp, status, etc) if (sendMessageListener != null) { sendMessageListener.onMessageSentComplete(message, null); } } } }); // save message on db if (sendMessageListener != null) { //set sender and recipiet because MessageListActivity use this message to update the view immediatly and MessageListAdapter use message.sender Log.d(TAG, "onBeforeMessageSent called with message : " + message); sendMessageListener.onBeforeMessageSent(message, null); } }
Example 6
Source File: SalesPendingPayButtonIntentService.java From stockita-point-of-sale with MIT License | 4 votes |
/** * Helper method to proceed the open bill and pass data into the server * @param userUid The user's uid * @param salesHeaderModel {@link SalesHeaderModel} instance * @param salesDetailModelList {@link SalesDetailModel} arrayList instance */ private void performOpenBill(String userUid, SalesHeaderModel salesHeaderModel, ArrayList<SalesDetailModel> salesDetailModelList) { // Get the node to /paidSalesHeader DatabaseReference salesHeaderRef = FirebaseDatabase.getInstance().getReference() .child(userUid) .child(Constants.FIREBASE_OPEN_SALES_HEADER_LOCATION); // Create the push() key DatabaseReference salesHeaderKey = salesHeaderRef.push(); String headerKey = salesHeaderKey.getKey(); // Pass the pojo salesHeaderKey.setValue(salesHeaderModel); /** * Store the {@link SalesDetailModel} into the server */ // The sales detail is already packed in the list, now we need the size of the list int sizeOfList = salesDetailModelList.size(); // Get the node to /paidSalesDetail DatabaseReference salesDetailRef = FirebaseDatabase.getInstance().getReference() .child(userUid).child(Constants.FIREBASE_OPEN_SALES_DETAIL_LOCATION) .child(headerKey); // Iterate for (int i = 0; i < sizeOfList; i++) { // Get the model form the list SalesDetailModel salesDetailModel = salesDetailModelList.get(i); // create a new push() key for each element in the list salesDetailRef.push().setValue(salesDetailModel); } // Now delete all data in the /salesDetailPending node. DatabaseReference salesDetailPendingRef = FirebaseDatabase.getInstance().getReference() .child(userUid) .child(Constants.FIREBASE_SALES_DETAIL_PENDING_LOCATION); // set the value to null means delete all salesDetailPendingRef.setValue(null); // Delete all data in the local database after ContentResolver contentResolver = getBaseContext().getContentResolver(); contentResolver.delete(ContractData.SalesDetailPendingEntry.CONTENT_URI, null, null); }