Send text message to WhatsApp and SMS using intent create chooser not working anymore - android

The code below used to work a week ago. It's purpose is to allow the user to choose if he want to send a text message using WhatsApp or SMS, but now when I choose WhatsApp it doesn't do anything, although SMS keeps working.
Looking at the logcat it's printing: 2018-10-25 18:28:28.915 2147-6714/? I/ActivityManager: START u0 {act=android.intent.action.SENDTO dat=smsto:xxxxxxxxxxx flg=0x3000000 cmp=com.whatsapp/.Conversation (has extras)} from uid 10096 Even passing a valid number with country code, it is printing smsto:xxxxxxxxxxx.
Is there any working code for this purpose or know about this problem?
fun sendMessageToNumber(number: String, text: String) {
val cleanNumber = number.cleanText()
val uri = Uri.parse("smsto:$cleanNumber")
val sendIntent = Intent(Intent.ACTION_SENDTO, uri)
sendIntent.putExtra("sms_body", text)
context?.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.fragment_account_chooser_message_title)))
}

In one of my project I have used following code for sending whatsapp message :
fun sendWhatsappMsg(){
var toNumber = "+91 xxxxx xxxxx" // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "")
val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.putExtra("jid", "$toNumber#s.whatsapp.net")
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hello")
sendIntent.action = Intent.ACTION_SEND
sendIntent.setPackage("com.whatsapp")
sendIntent.type = "text/plain"
startActivity(sendIntent)
}
And to send text message I have used following code :
fun sendTextMsg(){
val phone = "xxxxxxxxxx"
val msg = "smsto:" + phone
val smsUri = Uri.parse(msg)
val smsIntent = Intent(Intent.ACTION_SENDTO, smsUri)
startActivity(smsIntent)
}
You can try this. Both are working for me.

Related

how to handle share text on whatsapp if more than one whatsapp exist in android programatically?

I am sharing text on WhatsApp using this code. By doing this I can only share on WhatsApp messenger but what if users have more than one WhatsApp install like WA Messenger, WhatsApp business, etc?
I searched a lot but couldn't find a solution, if anyone knows put your answer here this will help a lot of people.
private fun shareLink(mobile: String, msg: String) {
try {
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
shareIntent.setPackage("com.whatsapp")
shareIntent.putExtra(Intent.EXTRA_TEXT, url)
startActivity(shareIntent)
}catch (e:PackageManager.NameNotFoundException){
e.printStackTrace()
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")
)
)
}
}

Android Email Intent doesn't populate recipients

I am having an issue with Email Intent in Android.
The recipients field is not populating properly.
My code is as such:
Extensions.kt
// Returns a Mail Intent
fun requireMailIntent(subject: String, body: String) = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("email#gmail.com"))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT,body)
}
/**
* Checks whether the intent has an app that can
* handle it.
* Should be called before starting an intent
**/
fun Intent.hasSuccessor(context: Context) = resolveActivity(context.packageManager) != null
Fragment.kt
// Submit button
binding.ButtonSubmit.setOnClickListener {
val emailConstruct = constructEmail()
val intent = requireMailIntent(emailConstruct.first, emailConstruct.second)
if(intent.hasSuccessor(requireContext())){
Log.v("INTENT_TEST", "Launching Intent")
startActivity(intent)
}else{
Log.v("INTENT_TEST", "No app found")
}
}
private fun constructEmail(): Pair<String,String>{
val subject = "MES :: Bug Report :: ${viewModel.bugIdentified}"
val message = "Below are the steps \n ${viewModel.bugSteps}"
return Pair(subject, message)
}
Manifest.xml
<!-- For basic package querying, ie browsers, email... -->
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
Gradle
// SDK Versions
sdk_compiled_version = 30
sdk_minimum_version = 24
// Build tools
build_tools = "29.0.3"
Upon clicking the button, Android shows me the app chooser, but when i click Gmail, everything populates except the recipients field.
Can someone please help ?
Try using URI
val uriText = "mailto:contact#example.com" +
"?subject=" + "your subject here" +
"&body=" + body
val uri = Uri.parse(uriText)
val sendIntent = Intent(Intent.ACTION_SENDTO)
sendIntent.data = uri
startActivity(Intent.createChooser(sendIntent, "Send Email").addFlags(FLAG_ACTIVITY_NEW_TASK))

Opening a WhatsApp chat with a specific contact by clicking on a button in my app (Kotlin)

How can I Open a Whatsapp chat with a specific contact by clicking on a button in my app?
That's the code I use. It opens WhatsApp and let me search the contact I want to send the message to, but it doesn't open the WhatsApp chat with the specific contact number I gave it to.
whatsappButton.setOnClickListener{
var con = itemView.context
val textToShare = "*כח אדם*"
val phoneNumber = blogPost.phone
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.type = "text/plain"
sendIntent.putExtra("jid", phoneNumber+"#s.whatsapp.net")
sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare)
val extra = sendIntent.extras
startActivity(con,sendIntent,extra)
}
If you want send a message to a specific contact from Contacts app, you should first require permission to access contacts, get the number and try with this:
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"#s.whatsapp.net");
startActivity(sendIntent);
Ref: https://stackoverflow.com/a/40285262/2895571
Please check this answer here
Use the following bit of code:
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp"); // so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);
First option: using Uri to convert whatsapp web url into button:
open_whatsapp.setOnClickListener {
val url = "https://api.whatsapp.com/send?phone=XXXXXXXXXX"
val openWhatsappIntent = Intent(Intent.ACTION_VIEW)
openWhatsappInten.data = Uri.parse(url)
startActivity(openWhatsappInten)
}
Second option; this is used as a href in web development :
Tel: XXX XXXX XX
maybe you can add Html format into TextView (Html.fromHtml()) and enable links clickeable to open the whatsapp application.
if you want send message to particular contact in whatsapp , use below url and particular mobile no to the Intent(including Country code).
String url = "https://wa.me/";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url+mobile_no));
startActivity(browserIntent);

Pre fill SMS in facebook messenger

I would like to pre fill a SMS for user who uses facebook messenger.
Here you can find my Kotlin code :
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("sms:" + smsToString)
intent.putExtra("sms_body", provider.getSmsBody())
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val list = context!!.packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY)
val context = activity!!.applicationContext
if (list.isNotEmpty()) {
context.startActivity(intent)
}
This code launch the facebook messenger app with the new sms window, and with the list of contacts prefilled.
But, the sms_body is empty. The same code works when the default sms app is Android Messages.
Is there a solution ?
Thanks :)

How to send message to particular whatsapp contact programmatically using intent? [duplicate]

This question already has answers here:
Send text to specific contact programmatically (whatsapp)
(29 answers)
Closed 6 years ago.
I searched various answers but all of them are outdated.
Whenever I tried using send to it only opens contact chooser.
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"#s.whatsapp.net");//phone number without "+" prefix
startActivity(sendIntent);
Update:
The aforementioned hack cannot be used to add any particular message, so here is the new approach. Pass the user mobile in international format here without any brackets, dashes or plus sign. Example: If the user is of India and his mobile number is 94xxxxxxxx , then international format will be 9194xxxxxxxx. Don't miss appending country code as a prefix in mobile number.
private fun sendMsg(mobile: String, msg: String){
try {
val packageManager = requireContext().packageManager
val i = Intent(Intent.ACTION_VIEW)
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
i.setPackage("com.whatsapp")
i.data = Uri.parse(url)
if (i.resolveActivity(packageManager) != null) {
requireContext().startActivity(i)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Note: This approach works only with contacts added in user's Whatsapp
account.
You have to set the package name in the Intent like this
intent.setPackage("com.whatsapp");
Full example:
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);
i.setPackage("com.whatsapp");
startActivity(i);
You just fire the below intent on the click on button:
Uri mUri = Uri.parse("smsto:" + mobile1);
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat", true);
startActivity(Intent.createChooser(mIntent, ""));
if number available on whatsapp then that particular user chat open and you send your message.If number not available on whatsapp the alert diaolge open in that case.
hope this help you ;-)
Here is solution
private void openWhatsApp(String number) {
String whatsAppMessage = "Hello!";
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);
}
Call above function and pass number, by which by want to open chat in Whatsapp messenger.
Hope it will work for you. :)

Categories

Resources