简体   繁体   中英

Send message body and phone number to Viber

I try to get message and phone number in my app and then send a message to that number, I want to send this message via Viber application. I can send message with this code :

     Intent intent = new Intent(Intent.ACTION_SEND);    
     intent.setType("text/plain"); 
     intent.putExtra(android.content.Intent.EXTRA_TEXT, "test test test"); 
     intent.setpackage("com.viber.voip");
     startActivity(intent);

How can I send phone number to Viber?

There are two ways to send a message using Intents to Viber.

Option A - will not fill in sms_body unfortunately. But will open directly dialog with specific contact:

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setPackage("com.viber.voip");
smsIntent.setData(Uri.parse("sms:+1001002003"));
smsIntent.putExtra("address", "+1001002003");
smsIntent.putExtra("sms_body", "body  text");
startActivity(smsIntent);

Option B - will give you an option which user should receive the message:

Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.viber.voip");
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "Message body");
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        smsIntent.setData(Uri.parse("smsto:"));
        smsIntent.setType("vnd.android-dir/mms-sms");
        smsIntent.putExtra("address", phoneNumber);
        smsIntent.putExtra("sms_body", "body  text"); 
        startActivity(smsIntent);   

phoneNumber - is to whoom you want to send the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM