我正在使用Cordova插件短信,它运行良好。我可以发送短信,但每次短信不能超过140-70个字符。我需要像默认的短信应用程序一样发送二合一的短信。对于超过大小的短信,它会发出一个成功的回调,但短信不会发送。
https://github.com/floatinghotpot/cordova-plugin-sms
var successCallback = function () {
msgSentUser(message);
};
var failureCallback = function (e) {
};
SMS.sendSMS(number, fullMsg, successCallback, failureCallback);
谢谢你,
查看该插件的源代码,可以看到它使用了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);
}
下面的替换块将消息划分为适当的部分,并创建必要的PendingIntent
s的ArrayList
以传递给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;
}
这个方法现在将如下。请注意,该方法的参数类型已更改,BODY
键的JSONObject
值也已更改。
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对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);
}
}
它基本上在第一个"onsmsreach"事件后等待1秒,以连接所有接收到的短信(因为每个短信都需要>1s才能发送),它应该能在上工作
问题似乎出现在:
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已被注释,它也独立于SMS发送。
我选择了更改插件。我得到了一个发送大短信的工作:https://github.com/cordova-sms/cordova-sms-plugin,问题是这个没有startwatch。我正在尝试添加它(正如你所看到的,我是插件和JS的新手)