javaandroidsmstelephonymanager

How to sendMultiPartTextMessage in Java


i'm trying to modify an existing plugin to enable it send long text messages. I am not so familiar with java, but I found some examples online and followed them. However i'm getting an error incompatible types: SendSMSModule cannot be converted to Context deliveredPIarr.add(PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0));

incompatible types: SendSMSModule cannot be converted to Context sentPIarr.add(PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0));

This is my java code

private void sendDirect(ReadableMap options, Callback callback) {

        String msg = options.hasKey("body") ? options.getString("body") : "";
        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);

        ReadableArray recipients = options.hasKey("recipients") ? options.getArray("recipients") : null;
        for (int i = 0; i < recipients.size(); i++) {
            String phoneNo = recipients.getString(i);
            try {
                SmsManager smsManager = SmsManager.getDefault();
                if (msg.length() > 160) {
                    ArrayList<String> parts = smsManager.divideMessage(msg);

                    ArrayList<PendingIntent> sentPIarr = new ArrayList<PendingIntent>();
                    ArrayList<PendingIntent> deliveredPIarr = new ArrayList<PendingIntent>();

                    for (int m = 0; m < parts.size(); m++) {
                        sentPIarr.add(PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0));
                        deliveredPIarr.add(PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0));
                    }

                    smsManager.sendMultipartTextMessage(phoneNo, null, parts, sentPIarr, deliveredPIarr);
                } else {
                    smsManager.sendTextMessage(phoneNo, null, msg, null, null);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                sendCallback(false, false, true);
                return;
            }
        }

        sendCallback(true, false, false);

    }
}

Solution

  • This worked for me

    private void sendDirect(ReadableMap options, Callback callback) {
    
            String msg = options.hasKey("body") ? options.getString("body") : "";
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";
    
            ReadableArray recipients = options.hasKey("recipients") ? options.getArray("recipients") : null;
            for (int i = 0; i < recipients.size(); i++) {
                String phoneNo = recipients.getString(i);
                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    if (msg.length() > 160) {
                        ArrayList<String> parts = smsManager.divideMessage(msg);
    
                        smsManager.sendMultipartTextMessage(phoneNo, null, parts, null, null);
                    } else {
                        smsManager.sendTextMessage(phoneNo, null, msg, null, null);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    sendCallback(false, false, true);
                    return;
                }
            }
    
            sendCallback(true, false, false);
    
        }
    }