我正在使用Cordova-plugin-sms,它工作得很好。我可以发送短信,除非我不能超过每个短信标准140-70个字符。我需要发送2合1短信喜欢默认的短信应用程序。对于超过大小的短信,会给出successCallback,但不会发送短信。
https://github.com/floatinghotpot/cordova-plugin-sms
var successCallback = function () {
msgSentUser(message);
};
var failureCallback = function (e) {
};
SMS.sendSMS(number, fullMsg, successCallback, failureCallback);谢谢,
发布于 2016-03-23 19:45:44
查看该插件的source code,您可以看到它使用了SmsManager#sendTextMessage()方法。此方法只处理由单个部分组成的消息,如果传递给它的消息超过了所使用的字母表中单个部分的字符限制,它将以静默方式失败。但是,您仍然可以获得successCallback,因为没有抛出Exception,而且插件本身也不使用其他确认方法。解决方案是修改代码以使用sendMultipartTextMessage()方法。
在原始源代码中,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的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;
}发布于 2016-03-24 22:54:17
对于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);
}
}它基本上在第一个"onsmsarrive“事件后等待1秒,以合并所有收到的短信(因为每条短信发送需要>1秒),它应该可以工作
发布于 2016-03-28 18:58:23
看起来问题出在:
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,也可以单独发送短信。
https://stackoverflow.com/questions/36161048
复制相似问题