【发布时间】:2021-07-16 02:03:45
【问题描述】:
我试图获取用户朋友的密钥或 ID,然后向他们所有人发送通知,但是当我重视他们时
这是我的代码
val shared = requireActivity().getSharedPreferences("user_phone", Context.MODE_PRIVATE)
val currentUserphone = shared.getString("phone","default_value")
Log.d("friendId", "currentUserphone"+""+currentUserphone)
val ref = Firebase.database.reference.child("users").child(currentUserphone!!)
.child("friends").addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (i in snapshot.children) {
//val model = snapshot.getValue(friendsIdsModel::class.java)
val id = snapshot.child("phone").getValue(String::class.java)
//idsList.add(model!!)
sendNotifcatiosToFriends(id!!)
Log.d("friendId", "fried Id" + "" + id)
// Log.d("friendId", "list position"+""+idsList.size)
}
}else {
Log.d("friendId", "snapshot not exist")
}
}
override fun onCancelled(error: DatabaseError) {
Log.d("friendId", "error"+error.message.toString())
}
})
private fun sendNotifcatiosToFriends( friendId : String) {
if(friendId != null) {
val map: HashMap<String, Any> = HashMap()
map.put("tittle", "notification tittle")
map.put("body", "notification body")
val ref = Firebase.database.reference.child("users").child(friendId)
.child("notifications")
ref.updateChildren(map)
}else{
Log.d("friendId", "friend id is null")
}
}
【问题讨论】:
标签: android firebase kotlin firebase-realtime-database