How to Open Specific Contact Chat Screen in Various Popular Chat/Social-Networks Apps

How to open specific contact chat screen in various popular chat/social-networks apps?

For Facebook-messenger, I've found this (from https://developers.facebook.com/docs/messenger-platform/discovery/m-me-links#format):

final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.me/" + facebookId));

It works, but I wonder if there is another way to access it (using phone number, for example).


For WhatsApp, I've found this (from here) :

    final String formattedPhoneNumber = getFormattedPhoneNumber(this, phone);
final String contactId = getContactIdFromPhoneNumber(phone);
final String contactMimeTypeDataId = getContactMimeTypeDataId(contactId, "vnd.android.cursor.item/vnd.com.whatsapp.profile");
if (contactMimeTypeDataId != null) {
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + formattedPhoneNumber));
intent.setPackage("com.whatsapp");
} else
Toast.makeText(this, "cannot find this contact on whatsapp", Toast.LENGTH_SHORT).show();

public static String getFormattedPhoneNumber(Context context, String input) {
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String normalizedPhone = input.replaceAll("[^0-9+]", "");
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
final PhoneNumber phoneNumber = phoneNumberUtil.parse(normalizedPhone, countryCode.toUpperCase());
final String formattedPhoneNumber = phoneNumberUtil.format(phoneNumber, PhoneNumberFormat.E164).replaceAll("[^0-9]", "");
return formattedPhoneNumber;
} catch (NumberParseException e) {
e.printStackTrace();
}
return null;
}

private String getContactIdFromPhoneNumber(String phone) {
if (TextUtils.isEmpty(phone))
return null;
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
final ContentResolver contentResolver = getContentResolver();
final Cursor phoneQueryCursor = contentResolver.query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if (phoneQueryCursor != null) {
if (phoneQueryCursor.moveToFirst()) {
String result = phoneQueryCursor.getString(phoneQueryCursor.getColumnIndex(PhoneLookup._ID));
phoneQueryCursor.close();
return result;
}
phoneQueryCursor.close();
}
return null;
}

public String getContactMimeTypeDataId(@NonNull Context context, String contactId, @NonNull String mimeType) {
if (TextUtils.isEmpty(mimeType))
return null;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{Data._ID}, Data.MIMETYPE + "= ? AND "
+ ContactsContract.Data.CONTACT_ID + "= ?", new String[]{mimeType, contactId}, null);
if (cursor == null)
return null;
if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
String result = cursor.getString(cursor.getColumnIndex(Data._ID));
cursor.close();
return result;
}

It works, but it doesn't add the message. It also might say the contact doesn't have WhatsApp.

It's also possible to just use the phone number, as I wrote here.


For Viber, I've found this (from here) :

        final String contactId = getContactIdFromPhoneNumber(phone);
final String contactMimeTypeDataId = getContactMimeTypeDataId(contactId, "vnd.android.cursor.item/vnd.com.viber.voip.viber_number_message");
if (contactMimeTypeDataId != null) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + contactMimeTypeDataId));
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setPackage("com.viber.voip");
} else {
intent = new Intent("android.intent.action.VIEW", Uri.parse("tel:" + Uri.encode(formattedPhoneNumber)));
intent.setClassName("com.viber.voip", "com.viber.voip.WelcomeActivity");
}

private String getContactIdFromPhoneNumber(String phone) {
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
final ContentResolver contentResolver = getContentResolver();
final Cursor phoneQueryCursor = contentResolver.query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if (phoneQueryCursor != null) {
if (phoneQueryCursor.moveToFirst()) {
String result = phoneQueryCursor.getString(phoneQueryCursor.getColumnIndex(PhoneLookup._ID));
phoneQueryCursor.close();
return result;
}
phoneQueryCursor.close();
}
return null;
}

For Hangouts, it seems it's similar to Viber, but with this mimetype: "vnd.android.cursor.item/vnd.googleplus.profile.comm". Yet, it doesn't work as it probably needs additional steps (setting G+ to keep contacts updated and have the contacts in the G+ circles). However, I've somehow succeeded to open the video chat of a person:

        intent =new Intent(Intent.ACTION_VIEW,Uri.parse("content://com.android.contacts/data/"+contactMimeTypeDataId));
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);

For Telegram, someone (here) suggested using the next code, but it doesn't work:

        intent = new Intent(android.content.Intent.ACTION_SENDUri.parse("http://telegram.me/"+profile)));
intent.setPackage("org.telegram.messenger");

It's also possible to just use the phone number, as I wrote here.

For Line, I've found these (based on here and here), but none work:

    Intent intent = new Intent("jp.naver.line.android.intent.action.LINESHORTCUT");
intent.putExtra("shortcutType", "chatmid");
intent.putExtra("shortcutTargetId", target);
intent.putExtra("shortcutTargetName", "");
intent.putExtra("shortcutFromOS", false);
startActivity(intent);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("line://msg/text/" + getMongon()));

skype: this one works (found from various links, here, for example):

        final String skypeUserName = getSkypeUserName(phone);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("skype:" + skypeUserName + "?chat"));

public String getSkypeUserName(String phoneNumber) {
if (TextUtils.isEmpty(phoneNumber))
return null;
ContentResolver cr = getContentResolver();
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.LOOKUP_KEY}, null, null, null);
if (cursor == null)
return null;
final Set<String> contactKeys = new HashSet<>();
// get contact keys
{
final int contactKeyIdx = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String contactKey = cursor.getString(contactKeyIdx);
contactKeys.add(contactKey);
}
cursor.close();
}
if (contactKeys.isEmpty())
return null;
//get raw ids
final Set<String> contactRawIdsSet = new HashSet<>();
{
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < contactKeys.size(); ++i)
sb.append(sb.length() == 0 ? "?" : ",?");
String inParameters = sb.toString();
final String[] selectionArgs = contactKeys.toArray(new String[contactKeys.size()]);
cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data.RAW_CONTACT_ID}, ContactsContract.Data.LOOKUP_KEY + " IN (" + inParameters + ")", selectionArgs, null);
if (cursor == null)
return null;
final int rawContactColIdx = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String rawContactId = cursor.getString(rawContactColIdx);
contactRawIdsSet.add(rawContactId);
}
cursor.close();
}
if (contactRawIdsSet.isEmpty())
return null;
//find the skype name
//TODO think of a better way to query, as it looks weird to search within a set of ids...
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < contactRawIdsSet.size(); ++i)
sb.append(sb.length() == 0 ? "?" : ",?");
String inParameters = sb.toString();
final String[] selectionArgs = new String[2 + contactRawIdsSet.size()];
selectionArgs[0] = "com.skype.contacts.sync";
selectionArgs[1] = "vnd.android.cursor.item/name";
int i = 2;
for (String rawId : contactRawIdsSet)
selectionArgs[i++] = rawId;
cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{RawContacts.SOURCE_ID}, ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND " + Data.MIMETYPE + " = ? AND " +
ContactsContract.Data.CONTACT_ID + " IN (" + inParameters + ")", selectionArgs, null);
if (cursor == null)
return null;
if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
String result = cursor.getString(cursor.getColumnIndex(RawContacts.SOURCE_ID));
cursor.close();
return result;
}

How can I open WhatsApp's conversation activity using contact data?

private void openWhatsApp(String id) {

Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
new String[] { id }, null);
c.moveToFirst();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));

startActivity(i);
c.close();
}

Where id is what's app uri like 0987654321@s.whatsapp.net

Start Facebook Chat with Another user using Facebook ID

The answer is in the first comment of the other thread: That solution does not work with App Scoped IDs, and you don´t get the real IDs anymore. Even if it would be possible, you can only get App Scoped IDs of friends who authorized your App too. Meaning, it´s impossible.

How can I send message to specific contact through WhatsApp from my android app?

Try using Intent.EXTRA_TEXT instead of sms_body as your extra key. Per WhatsApp's documentation, this is what you have to use.

An example from their website:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

Their example uses Intent.ACTION_SEND instead of Intent.ACTION_SENDTO, so I'm not sure if WhatsApp even supports sending directly to a contact via the intent system. Some quick testing should let you determine that.

Sending message through WhatsApp

UPDATE
Please refer to https://faq.whatsapp.com/en/android/26000030/?category=5245251

WhatsApp's Click to Chat feature allows you to begin a chat with
someone without having their phone number saved in your phone's
address book. As long as you know this person’s phone number, you can
create a link that will allow you to start a chat with them.

Use: https://wa.me/15551234567

Don't use: https://wa.me/+001-(555)1234567

Example: https://wa.me/15551234567?text=I'm%20interested%20in%20your%20car%20for%20sale

Original answer
Here is the solution

public void onClickWhatsApp(View view) {

PackageManager pm=getPackageManager();
try {

Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";

PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");

waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));

} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}

}

Also see http://www.whatsapp.com/faq/en/android/28000012



Related Topics



Leave a reply



Submit