Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when send two or more messages #132

Closed
daviddguedes opened this issue Apr 18, 2017 · 9 comments
Closed

Error when send two or more messages #132

daviddguedes opened this issue Apr 18, 2017 · 9 comments
Assignees

Comments

@daviddguedes
Copy link

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!');
});
@dbaq
Copy link
Member

dbaq commented Apr 18, 2017

Hey @daviddguedes, I don't see anything wrong with your code. Have you figured this out?

@dbaq dbaq self-assigned this Apr 18, 2017
@daviddguedes
Copy link
Author

@dbaq I have not figured out what might be wrong. Still unsolved.

@mbaaro
Copy link

mbaaro commented May 8, 2017

I also face the same issue of the message either taking too long or one number receiving two messages....
Anyone with a solution to this Please help....
Or is there a way to clear/refresh the plugin after every message is sent??

@gotling
Copy link
Contributor

gotling commented 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)

@dbaq
Copy link
Member

dbaq commented Jan 25, 2018

Yes, please loop over and call N times the sms.send function.

@dbaq dbaq closed this as completed Jan 25, 2018
@gotling
Copy link
Contributor

gotling commented 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.

@dbaq dbaq reopened this Jan 25, 2018
@dbaq
Copy link
Member

dbaq commented Jan 25, 2018

Ok I was thinking about another issue, I am reopening this. I will try to take a look soon.

@acedigibits
Copy link

@gotling gave a good solution. Unfortunately those multiple instances in loop are too confusing and sometimes work erraneously.

Why cant multiple numbers work like for this plugin:

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

This above plugin doesnt support iOS hence i am forced to use your plugin.

@dbaq
Copy link
Member

dbaq commented Feb 1, 2018

Hi @acedigibits,

You can contribute by opening a PR in the meantime, cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants