Reputation: 2069
I want to send message to multiple contacts from mobile.Flow is that Iam creating a Blood Bank application in which when user request a blood I show him the list of all user with matched blood group in firebase recycler view.In recycler,Iam showing him the name of user with 2 icon of call and message on click on any icon required process is working fine. What I want is when user request blood after showing him the list of matched blood group person I should pragmatically send message to all users those are shown in the list of matched blood group. I have viewed multiple question and and tried there answers but didn't get success to achieve it.
In this I have tried another solution from stack overflow but the result is same it didn't send message
private RecyclerView recyclerView;
private DatabaseReference reference;
String bloodGroup;
LinearLayout linearLayout;
ProgressDialog progressDialog;
FirebaseRecyclerOptions<Profiles> model;
FirebaseRecyclerAdapter<Profiles, DonarViewHolder> adapter;
ArrayList<String> contactNumber = new ArrayList<>();
HashSet<String> numbersSet = new HashSet<>();
int MY_PERMISSIONS_REQUEST_SEND_SMS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donar_list);
getView();
showRecyclerView();
sendMsg();
contactNumber.addAll(numbersSet);
}
private void sendMsg() {
for (int i = 0; i < contactNumber.size(); i++)
{
String message = "Blood Required Urgently";
String tempMobileNumber = contactNumber.get(i).toString();
MultipleSMS(tempMobileNumber, message);
}
}
private void MultipleSMS(String phoneNumber , String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValues values = new ContentValues();
for (int i = 0; i < contactNumber.size() - 1; i++) {
values.put("address", contactNumber.get(i).toString());
// txtPhoneNo.getText().toString());
values.put("body", "Blood Required Urgently");
}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(DonarList.this, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(DonarList.this, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(DonarList.this, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(DonarList.this, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(DonarList.this, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(DonarList.this, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(DonarList.this, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
private void showRecyclerView() {
adapter = new FirebaseRecyclerAdapter<Profiles, DonarViewHolder>(model) {
@Override
protected void onBindViewHolder(@NonNull DonarViewHolder holder, int position, @NonNull Profiles profiles) {
String check = profiles.getBloodBank();
String number = profiles.getMobile_number();
numbersSet.add(profiles.getMobile_number());
System.out.println("all" + numbersSet);
ArrayList<String> bloodGroupMatched = new ArrayList<>();
bloodGroupMatched.addAll(profiles.getMatched_bloodGroups());
System.out.println("check" + bloodGroupMatched);
if (bloodGroupMatched.contains(bloodGroup)) {
holder.name.setText(profiles.getName());
holder.itemView.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 5, 5, 10);
holder.itemView.setLayoutParams(params);
} else {
holder.itemView.setVisibility(View.GONE);
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
holder.call.setOnClickListener(v -> {
boolean conditionCheck = true;
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(DonarList.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(DonarList.this,
new String[]{Manifest.permission.CALL_PHONE}, 1);
}
startActivity(callIntent);
});
holder.msg.setOnClickListener(v -> {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + number));
if (ActivityCompat.checkSelfPermission(DonarList.this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(DonarList.this,
new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
} else {
startActivity(smsIntent);
}
});
}
@NonNull
@Override
public DonarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_donar_view_holder, parent, false);
return new DonarViewHolder(view);
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter.startListening();
progressDialog.dismiss();
recyclerView.setAdapter(adapter);
}
private void getView() {
progressDialog = HelperClass.showProgressDialog(this, "Loading..");
if (getIntent() != null && getIntent().hasExtra("bloodGroup")) {
bloodGroup = getIntent().getStringExtra("bloodGroup");
Toast.makeText(this, bloodGroup, Toast.LENGTH_SHORT).show();
}
recyclerView = findViewById(R.id.donar_list_recycler);
linearLayout = findViewById(R.id.recylerLayout);
reference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles);
model = new FirebaseRecyclerOptions.Builder<Profiles>()
.setQuery(reference, Profiles.class).build();
}
@Override
public void onStart() {
super.onStart();
if (adapter != null) {
adapter.startListening();
}
}
@Override
public void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
@Override
protected void onResume() {
super.onResume();
if (adapter != null) {
adapter.startListening();
}
}
}
Note : Iam trying to send message to multiple users not a single user
Upvotes: 0
Views: 277
Reputation: 2069
I have found 2 solution for this.
1-while getting the data from firebase I send the message right away to user by putting message code where Iam getting user detail(it will get 1 number and sent message to it then the other then the other and after that it will set the adapter and yes during this I will show some progress bar or something).
2-The other thing which I can do in my app is show the button of send message to user below list and store all the contact number to array list while getting user data and when user click on that button send message to all user those number we have get in arrayList.
like this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donar_list);
getView();
showRecyclerView();
contactNumber.addAll(numbersSet);
}
private void showRecyclerView() {
adapter = new FirebaseRecyclerAdapter<Profiles, DonarViewHolder>(model) {
@Override
protected void onBindViewHolder(@NonNull DonarViewHolder holder, int position, @NonNull Profiles profiles) {
String number = profiles.getMobile_number();
numbersSet.add(profiles.getMobile_number());
ArrayList<String> bloodGroupMatched = new ArrayList<>();
bloodGroupMatched.addAll(profiles.getMatched_bloodGroups());
if (bloodGroupMatched.contains(bloodGroup)) {
holder.name.setText(profiles.getName());
holder.itemView.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 5, 5, 10);
holder.itemView.setLayoutParams(params);
} else {
holder.itemView.setVisibility(View.GONE);
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
holder.call.setOnClickListener(v -> {
boolean conditionCheck = true;
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(DonarList.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(DonarList.this,
new String[]{Manifest.permission.CALL_PHONE}, 1);
}
startActivity(callIntent);
});
holder.msg.setOnClickListener(v -> {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + number));
if (ActivityCompat.checkSelfPermission(DonarList.this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(DonarList.this,
new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS_REQUEST_SEND_SMS);
} else {
startActivity(smsIntent);
}
});
}
@NonNull
@Override
public DonarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_donar_view_holder, parent, false);
return new DonarViewHolder(view);
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter.startListening();
progressDialog.dismiss();
recyclerView.setAdapter(adapter);
}
public void sendSMS() {
contactNumber.addAll(numbersSet);
try {
SmsManager smsManager = SmsManager.getDefault();
for (int i = 0; i < contactNumber.size(); i++) {
smsManager.sendTextMessage(contactNumber.get(i), null, "Blood Required!!!+_", null, null);
}
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
private void getView() {
progressDialog = HelperClass.showProgressDialog(this, "Loading..");
if (getIntent() != null && getIntent().hasExtra("bloodGroup")) {
bloodGroup = getIntent().getStringExtra("bloodGroup");
Toast.makeText(this, bloodGroup, Toast.LENGTH_SHORT).show();
}
recyclerView = findViewById(R.id.donar_list_recycler);
button = findViewById(R.id.Mmsgs);
linearLayout = findViewById(R.id.recylerLayout);
reference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles);
model = new FirebaseRecyclerOptions.Builder<Profiles>()
.setQuery(reference, Profiles.class).build();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
}
}
Upvotes: 0