Search code examples
smsnativescriptnativescript-plugin

nativescript-phone prevents Nativescript-contacts from returning


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.


Solution

  • 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;
            })
        })
    }