firebase java_Java Firebase.push方法代码示例
import com.firebase.client.Firebase; //导入方法依赖的package包/类/*** Setup Firebase for the user and connected devices** Update the existing user data with the number of devices selected on the previous scree
import com.firebase.client.Firebase; //导入方法依赖的package包/类
/**
* Setup Firebase for the user and connected devices
*
* Update the existing user data with the number of devices selected on the previous screen
* Add the devices based on the selection
* Generate the message for the next device
*/
private void setupFirebase() {
// Update Firebase to include the total number of devices
if (!TextUtils.isEmpty(mFirebaseUid)) {
Map profile = new HashMap<>();
profile.put(Constants.FIREBASE_PROPERTY_NUMBER_DEVICES, mTotalDevices);
Firebase userRef = new Firebase(Constants.FIREBASE_URL_USERS).child(mFirebaseUid);
userRef.updateChildren(profile);
LogUtils.LOGE("***> setupFirebase", "update firebase uid:" + mFirebaseUid);
// Add device ids to Firebase
for (int i = 0; i < PreferencesUtils.getInt(mActivity, R.string.key_total_devices, 1); i++) {
// If the challengeCode is empty, then push a new value to the database
Firebase deviceRef = new Firebase(Constants.FIREBASE_URL_DEVICES).child(mFirebaseUid);
final Firebase newDeviceRef = deviceRef.push();
mDeviceIds[i] = newDeviceRef.getKey();
final int currentDeviceNumber = i + 1;
LogUtils.LOGE("***> new device #" + currentDeviceNumber, mDeviceIds[i]);
Device device = new Device(mDeviceIds[i], i + 1, mDeviceNum == currentDeviceNumber);
newDeviceRef.setValue(device);
newDeviceRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Device d = dataSnapshot.getValue(Device.class);
if (d != null) {
LogUtils.LOGE("***> child updated", "id: " + d.getDeviceId());
LogUtils.LOGE("***> child updated", "number: " + d.getDeviceNumber());
LogUtils.LOGE("***> child updated", "connected: " + d.isConnected());
LogUtils.LOGE("***> child updated", "next: " + currentDeviceNumber);
// Hide the button and show the message for the first device
if (d.isConnected() && d.getDeviceNumber() > 1) {
PreferencesUtils.setBoolean(mActivity, R.string.key_message_received, true);
LogUtils.LOGE("***> child updated", "unpublished: " + currentDeviceNumber);
unpublish();
mDiscover.setVisibility(View.GONE);
mIntroConnectDevice.setText(mActivity.getString(R.string.text_message_received));
}
} else {
if (!PreferencesUtils.getBoolean(mActivity, R.string.key_device_reset_done, false)) {
LogUtils.LOGE("***> setupFirebase", "reset device");
MainActivity.resetDevice();
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
// Create the message for the next device
if (i == mDeviceNum) {
LogUtils.LOGE("***> device", "message created");
String message = mActivity.getString(R.string.message_body, mFirebaseUid, mDeviceNum + 1,
mTotalDevices, newDeviceRef.getKey());
mDeviceInfoMessage = DeviceMessage.newNearbyMessage(
InstanceID.getInstance(mActivity.getApplicationContext()).getId(), message);
}
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)