question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when send two or more messages

See original GitHub issue

I am using the Cordova SMS plugin to send messages from an application developed with Ionic. When the “friends” array has length = 1 the SMS is sent. But when you have more than that, sometimes the messages do not arrive, or worse, the same number gets two messages. Is there something wrong with this code? The problematic part of the code is as follows:

$rootScope.showLoading('Enviando mensagen(s)!');

var loopPromises = [];

$rootScope.friends = [{
            nome: 'David',
            telefone: '83777777777'
        }, {
            nome: 'Edvan',
            telefone: '83444444444'
        }, {
            nome: 'Débora',
            telefone: '83888888888'
        }]

var interno = {
      nome: "#App",
      telefone: "83222222222"
}
$rootScope.friends.push(interno)

angular.forEach($rootScope.friends, function (a) {
  var deferred = $q.defer();
  loopPromises.push(deferred.promise);
  var texto = '';
  if (a.telefone == "83222222222") {
    texto = "Internal report"
    $rootScope.friends.pop();
  } else {
    texto = "Hello! I'm here!"
  }

  $cordovaSms.send(a.telefone, texto)
    .then(deferred.resolve, deferred.reject);
});

$q.all(loopPromises).then(function (results) {
  $rootScope.hideLoading();
  $rootScope.showToast('Messages sent!');
  }, function (errors) {
  $rootScope.hideLoading();
  $rootScope.showToast('Some message was not sent!');
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gotlingcommented, Jan 25, 2018

@dbaq Just looping over is not enough as multiple async calls to the underlying send function will be done, and the end result of that is unreliable. Doing the calls in sync does work.

1reaction
gotlingcommented, Jan 17, 2018

I noticed the same unreliable behavior when sending to multiple numbers. Interesting enough the plugin actually expects an array of numbers which it internally remakes into a comma or semicolon separated string before sending of to Android. But at least in my environment only the first number in the array receives the message.

See sms.js and Sms.java

Workaround

If waiting until the previous message was sent, successful or failed, before sending to the next number, everything works ok. I made a recursive function that does just that.

I need to support older devices so I have not used any nice ES6 features.

/**
 * Send to next number when first succeed or fail.
 *
 * @param {array} numbers to send to
 * @param {string} message to send
 * @param {object} options to sms plugin
 * @param {integer} [index=0] current index in numbers array
 */
function sendChained(numbers, message, options, index) {
  index = (typeof index !== 'undefined') ?  index : 0;
  if (index >= numbers.length) {
    return
  }

  sms.send(numbers[index], message, options,
    function() {
      console.log('notification.sendSms', numbers[index], 'ok')
      sendChained(numbers, message, options, ++index)
    },
    function() {
      console.error('notification.sendSms', numbers[index], 'failed')
      sendChained(numbers, message, options, ++index)
    }
  );
}

Call with

sendChained(numbers, message, options)
Read more comments on GitHub >

github_iconTop Results From Across the Web

"Error code 2" Can't send text messages
Please let us know if Advanced Messaging is turned on, go to Message App > Settings > Advanced Messaging > off. We recommend...
Read more >
Top 6 Ways to Fix Message Not Sent Error on Android
Top 6 Ways to Fix Message Not Sent Error on Android · 1. Check SIM/Account Balance · 2. Enable Airplane Mode · 3....
Read more >
Fix: Can't Send Text Message with T-mobile Error 2
If you can't send messages with T-mobile error 2, your phone might be too cluttered. · Try clearing the cache and delete some...
Read more >
Why did the person I sent a message to get duplicate ...
This indicates that Twilio sent just one message to the carrier. If your user received more than one, that means there was a...
Read more >
Error occurs when you send or receive an email message in ...
Lists various error messages that you may receive when you send or receive ... Method 2: Make sure that your Outlook email account...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found