我正在做一个考勤应用程序,用于存储学生的出勤率,我想偶尔将他们的出勤率发送给他们的父母.所以问题是当我从Firebase数据库中检索所有学生的姓名,百分比,电话号码并使用Textlocal API向他们发送SMS时,它仅向一个用户发送SMS,而其余用户则不发送.我很高兴有人帮助我.提前致谢.
DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference("Students");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (final DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
Student student=dataSnapshot1.getValue(Student.class);
final String name=student.getName();
final String phone=student.getPhone();
final String reg=student.getReg();
DatabaseReference databaseReference1=FirebaseDatabase.getInstance().getReference("Count");
databaseReference1.child(reg).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
int presentPeriods=dataSnapshot.getValue(int.class);
int totalPeriods=totalWorkingDays*7;
float percent=(float)presentPeriods/(float)totalPeriods*100;
int nPrecent=(int)percent;
sendSMS(name,phone,reg,nPrecent);
}
else {
int percent=0;
sendSMS(name,percent);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
private void sendSMS(String name,String phone,String reg,int percent) {
if (networkAvailable()){
Toast.makeText(this,"Sending alerts...",Toast.LENGTH_SHORT).show();
System.out.println("To: "+phone.replace("+91","")+" / "+"Your ward,"+name+" with Register Number "+reg.replace("R","")+" is having Attendance percentage of "+percent+" as on "+new SimpleDateFormat("dd-MM-yyyy",Locale.getDefault()).format(new Date()));
String out="Your ward,Locale.getDefault()).format(new Date());
String toNumber=phone.replace("+91","");
try {
// Construct data
String apiKey = "apikey=" + "XXXXXXXXXXX";
String message = "&message=" + out;
String sender = "&sender=" + "TXTLCL";
String numbers = "&numbers=" + toNumber;
// Send data
HttpURLConnection conn = (HttpURLConnection) new URL("https://api.textlocal.in/send/?").openConnection();
String data = apiKey + numbers + message + sender;
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length",Integer.toString(data.length()));
conn.getOutputStream().write(data.getBytes("UTF-8"));
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
stringBuffer.append(line);
}
rd.close();
Toast.makeText(this,stringBuffer.toString(),Toast.LENGTH_SHORT).show();
} catch (Exception e) {
System.out.println("Error SMS "+e);
Toast.makeText(this,"Error: "+e.toString(),Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(this,"No Internet Connection",Toast.LENGTH_SHORT).show();
}
}
最佳答案
触发databaseReference1.child(reg).addListenerForSingleValueEvent()时,电话已设置为上一个学生对象值.因此,无论何时触发databaseReference1侦听器,短信都会发送到列表中的最后一个电话值.
原文链接:https://www.f2er.com/android/531328.html相应地修改逻辑.