Cordova-plugin-sms太大的短信错误

时间:2016-03-22 17:17:59

标签: android cordova

我正在使用Cordova-plugin-sms,它工作正常。 我可以发送短信,除了我不能超过每个短信的标准140-70个字符。 我需要发送二合一短信,如默认短信应用程序。 对于超出大小的短信,它给出了successCallback,但不发送短信。

https://github.com/floatinghotpot/cordova-plugin-sms

var successCallback = function () {
    msgSentUser(message);
};
var failureCallback = function (e) {
};
SMS.sendSMS(number, fullMsg, successCallback, failureCallback);

谢谢,

4 个答案:

答案 0 :(得分:2)

查看该插件的source code,您可以看到它使用SmsManager#sendTextMessage()方法。此方法仅处理单部分消息,如果您传递的消息超出了您正在使用的字母表中单个部分的字符限制,则它将无声地失败。但是,您仍然可以获得successCallback,因为没有抛出Exception,并且插件本身不使用其他方法进行确认。解决方案是更改代码以使用sendMultipartTextMessage()方法。

在原始资源中,第206行到第212行(包括第206行和第212行)处理消息发送,这是我们需要替换的内容。也就是说,这些行:

PendingIntent sentIntent = PendingIntent.getBroadcast((Context)this.cordova.getActivity(),
    (int)0, (Intent)new Intent("SENDING_SMS"), (int)0);
SmsManager sms = SmsManager.getDefault();
for (int i = 0; i < n; ++i) {
    String address;
    if ((address = addressList.optString(i)).length() <= 0) continue;
    sms.sendTextMessage(address, null, text, sentIntent, (PendingIntent)null);
}

以下替换块将消息划分为相应的部分,并创建必要的ArrayList PendingIntent个传递给sendMultipartTextMessage()方法。请注意,如果您正在处理SENDING_SMS广播,它现在将针对每个消息部分触发一次,而不是每次发送一次,就像单部分消息一样。

SmsManager sms = SmsManager.getDefault();

ArrayList<String> parts = sms.divideMessage(text);
final int count = parts.size();
ArrayList<PendingIntent> sentPIs = new ArrayList<PendingIntent>(count);

int req = 0;
PendingIntent pi = null;

for (int i = 0; i < n; i++) {
    String address;
    if ((address = addressList.optString(i)).length() <= 0) continue;

    sentPIs.clear();

    for (int j = 0; j < count; j++) {
        req = i * count + j;
        pi = PendingIntent.getBroadcast((Context) this.cordova.getActivity(),
            req, new Intent("SENDING_SMS"), 0);

        sentPIs.add(pi);
    }

    sms.sendMultipartTextMessage(address, null, parts, sentPIs, null);
}

该插件中的传入消息处理不正确,并且会导致多部分消息显示为多个单独的消息。需要更改两个代码部分来解决此问题。第一行是第350至354行,包括:

for (int i = 0; i < pdus.length; ++i) {
    SmsMessage sms = SmsMessage.createFromPdu((byte[])((byte[])pdus[i]));
    JSONObject json = SMSPlugin.this.getJsonFromSmsMessage(sms);
    SMSPlugin.this.onSMSArrive(json);
}

我们改为:

JSONObject json = SMSPlugin.this.getJsonFromSmsMessage(pdus);
SMSPlugin.this.onSMSArrive(json);

接下来,我们需要改变getJsonFromSmsMessage()方法;第447至466行,包括:

private JSONObject getJsonFromSmsMessage(SmsMessage sms) {
    JSONObject json = new JSONObject();

    try {
        json.put( ADDRESS, sms.getOriginatingAddress() );
        json.put( BODY, sms.getMessageBody() );
        json.put( DATE_SENT, sms.getTimestampMillis() );
        json.put( DATE, System.currentTimeMillis() );
        json.put( READ, MESSAGE_IS_NOT_READ );
        json.put( SEEN, MESSAGE_IS_NOT_SEEN );
        json.put( STATUS, sms.getStatus() );
        json.put( TYPE, MESSAGE_TYPE_INBOX );
        json.put( SERVICE_CENTER, sms.getServiceCenterAddress());

    } catch ( Exception e ) {
        e.printStackTrace();
    }

    return json;
}

此方法现在如下。请注意,方法的参数类型已更改,JSONObject键的BODY值也已更改。

private JSONObject getJsonFromSmsMessage(Object[] pdus) {
    SmsMessage sms = null;
    StringBuilder sb = new StringBuilder();
    JSONObject json = new JSONObject();

    for (int i = 0; i < pdus.length; i++) {
        sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
        sb.append(sms.getMessageBody());
    }

    try {
        json.put(ADDRESS, sms.getOriginatingAddress());
        json.put(BODY, sb.toString());
        json.put(DATE_SENT, sms.getTimestampMillis());
        json.put(DATE, System.currentTimeMillis());
        json.put(READ, MESSAGE_IS_NOT_READ);
        json.put(SEEN, MESSAGE_IS_NOT_SEEN);
        json.put(STATUS, sms.getStatus());
        json.put(TYPE, MESSAGE_TYPE_INBOX);
        json.put(SERVICE_CENTER, sms.getServiceCenterAddress());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return json;
}

答案 1 :(得分:0)

对于sms concat,我使用了这个:(我使用这个因为1对1聊天)

var totalSms = "";
function timeToAdd(newSms) {
        totalSms = totalSms + newSms;
        if (totalSms == newSms) {      // only waits the first time
            window.setTimeout(
                function () {                 
                    msgReceived(totalSms);
                    addConversationMessage(totalSms, "sender");
                    totalSms = "";
                }, 1000);
        }
    }

它在第一次&#34; onsmsarrive&#34;之后基本等待1秒。事件连接所有收到的短信(因为每个短信需要> 1s发送)它应该工作

答案 2 :(得分:0)

似乎问题在于:

safesmsExport.sendSMS = function(address, text, successCallback, failureCallback) {

    var numbers;
    if( Object.prototype.toString.call( address ) === '[object Array]' ) {
        numbers = address;
    } else if(typeof address === 'string') {
        numbers = [ address ];
    } else {
        if(typeof failureCallback === 'function') {
            failureCallback("require address, phone number as string, or array of string");
        }
        return;
    }

    cordova.exec(successCallback, failureCallback, 'SMS', 'sendSMS', [numbers, text]);

};

这不是从smsPlugin.java调用函数sendSMS。即使smsPlugin sendSMS被评论,也可以单独发送短信。

答案 3 :(得分:0)

我选择更改插件。我得到这个发送大短信的工作:https://github.com/cordova-sms/cordova-sms-plugin,问题是这个没有开始。即时通讯试图添加它(我可以看到插件和JS的新功能)