【问题标题】:Textlocal API sends SMS only to one user and not to othersTextlocal API 仅向一个用户发送 SMS,而不向其他用户发送
【发布时间】:2019-02-06 06:05:11
【问题描述】:

我正在开发出勤应用程序,用于存储学生的出勤率,我想偶尔将他们的出勤率发送给他们的父母。所以问题是当我从 firebase 数据库中检索所有学生的姓名、百分比、电话号码并使用 Textlocal API 向他们发送短信时,它只向一个用户发送短信,而其余用户不发送短信。如果有人帮助我,我很高兴。提前致谢。

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,phone,reg,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, "+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 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();
    }
}

【问题讨论】:

  • 你检查过 DataSnapshot 的大小。
  • 是的。 datasnapshot 给出五组学生的数据。 SMS 仅发送给最后一名学生,前四名学生不发送。
  • 您需要使用调试器和断点来调试您的应用程序,以查看使用哪些值调用了哪些代码等。
  • 问题解决了吗?如果没有,请添加数据库结构,也请回复@AlexMamo

标签: java android firebase firebase-realtime-database


【解决方案1】:

databaseReference1.child(reg).addListenerForSingleValueEvent() 被触发时,phone 已经设置为最后一个student 对象值。因此,每当触发 databaseReference1 侦听器时,都会向您列表中的最后一个电话值发送短信。

相应地修改你的逻辑。

【讨论】:

  • 我认为它可以解决我的问题。能不能说清楚点??我不明白。谢谢...
猜你喜欢
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多