capacitor-community / contacts

Contacts Plugin for Capacitor
https://capacitor-community.github.io/contacts/
122 stars 54 forks source link

How to open a contact with default contact app after creating a contact #114

Open richterdennis opened 1 year ago

richterdennis commented 1 year ago

I can successfully create a contact with

const { contactId } = await Contacts.createContact({ contact: contactData });

How do I open the contact with the default contact app?

After researching for hours, the best I achieved is

AppLauncher.openUrl({
    url: 'com.google.android.contacts',
});

This opens the contact app on android but not the contact itself.

A dedicated function to open the new created contact would be nice. Something like this:

Contacts.openContact(contactId);
josuelmm commented 1 month ago

Any update?

josuelmm commented 1 month ago

` @PluginMethod public void openContact(PluginCall call) { if (!isContactsPermissionGranted()) { requestContactsPermission(call); } else { String contactId = call.getString("contactId");

    if (contactId == null) {
        call.reject("Parameter `contactId` not provided.");
        return;
    }

    try {
        // Crear un Intent para abrir el contacto con el contactId proporcionado
        Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
        Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Asegurarse de que la actividad se inicie en un nuevo task

        getContext().startActivity(intent);
        call.resolve();
    } catch (Exception e) {
        call.reject("Failed to open contact with id: " + contactId, e);
    }
}

} `