0

I have an app where I want to select a person from contacts and then send a text to that person. It works as expected for the first user, but after that the app never receives control after the contact is selected. I've isolated the problem to the Nativescript-phone plugin. If you simply call phone.sms() to send a text, and then call contacts.getContact(), the problem occurs. I see this on both Android and iOS.

I've created a sample app that demos the problem at https://github.com/dlcole/contactTester. The sample app is Android only. I've spent a couple days on this and welcome any insights.

Edit 4/21/2020:

I've spent more time on this and can see what's happening. Both plugins have the same event handler and same request codes:

nativescript-phone:

var SEND_SMS = 1001;
activity.onActivityResult = function(requestCode, resultCode, data) {

nativescript-contacts:

var PICK_CONTACT = 1001;
appModule.android.on("activityResult", function(eventData) {

What happens is that after invoking phone.sms, calling contacts.getContact causes control to return to the phone plugin, and NOT the contacts plugin. I tried changing phone's request code to 1002 but had the same results.

So, the next step is to determine how to avoid the collision of the event handlers.

David
  • 578
  • 3
  • 16
  • Sorry, Im unable to reproduce the issue. Every time I tap on get contact I see logs of picked contact. – Manoj Apr 20 '20 at 04:23
  • @manoj Even after tapping Send Text? I see the problem on all devices and emulators, iOS and Android. – David Apr 20 '20 at 23:09
  • @manoj So if it works for you but not me, that suggests it may be my build, yes? I've uninstalled the platforms, moved node_modules, and removed/re-added the {N} CLI, but with no luck. Anything else you can suggest? – David Apr 21 '20 at 01:09
  • Which OS version you are testing it with? Did you try placing a debugger or log statements inside getContact method of plugin see exactly where it stops. – Manoj Apr 21 '20 at 02:14
  • @Manoj - I updated the problem to include details I found while debugging both plugins. – David Apr 21 '20 at 14:02
  • @Manoj thanks for the answer - I'll experiment with that and perhaps make a pull request on the plugin. The iOS side appears to be a completely different problem but with similar symptoms. I'll post more after I do additional investigation. – David Apr 22 '20 at 00:57

1 Answers1

1

Instead of using activityResult event, nativescript-phone plugin overwrites the default activity result callback.

A workaround is to set the callback to it's original value after you are done with nativescript-phone.

exports.sendText = function (args) {
  console.log("entering sendText");

  const activity = appModule.android.foregroundActivity || appModule.android.startActivity;
  const onActivityResult = activity.onActivityResult;

  permissions.requestPermissions([android.Manifest.permission.CALL_PHONE],
    "Permission needed to send text")
    .then(() => {
      console.log("permission granted");
      phone.sms()
        .then((result) => {
          console.log(JSON.stringify(result, null, 4));
          activity.onActivityResult = onActivityResult;
        })
    })
}
Manoj
  • 21,753
  • 3
  • 20
  • 41
  • I've submitted a pull request to nativescript-phone that includes this fix. It will be included in the upcoming v2.0 update. This is for Android only. iOS has a separate problem with similar symptoms, and I'll create a new post for that as I investigate further. – David Apr 23 '20 at 19:17