Simple Notification in Android

Image
Notifications are a way to provide information to your customer which displays as a pop up on the device. It helps you to keep in touch with your customer. I am going to create a simple notification without going deep because everything is available on Google. We must understand a few concepts before going further. Notification channel:  Assume you have developed an app for an online learning center where they teach Maths and English. Students can either learn Maths, English, or Both. we must send notification related to Maths to only students who have taken Maths. In this case, we can create channels for Maths, English, and both so triggered notification will be delivered to students who have taken that subject. Each Notification channel can have a different Notification sound and different behavior that you can change from the Notification setting page. I forgot to tell you that Google has introduced the Notification channel from Android 8.0 i.e API level 26. Now yo...

SMS in Android



Sending SMS in Android
======================

SMS is one of the most used and important function in android.It can be used to share small amount of data between people.SMs can be used to send message to your friend,Increase Sales and Brand exposure,New Revenue Source for your business,Mass Alert to everyone etc.

Sending SMS using Intent
========================

Using built in functionality ofAndroid,We can send SMS through Intent.


   Intent sendsms = new Intent(Intent.ACTION_VIEW);
   sendsms.setType("vnd.android-dir/mms-sms");
   sendsms.putExtra("address", "+919912345678");
   sendsms.putExtra("sms_body","hey wassuppp ?????");
   startActivity(sendsms);

When call startActivity() with action Intent.ACTION_VIEW, the system will start an activity to display the data to the user.Android knows based upon the specified "type"("vnd.android-dir/mms-sms") that it is meant for the built-in SMS Activity.The "address" and "sms_body" extras specify the phone number and the body of the message that will be filled in when the Activity launches.

For Example:

   public class sendsms extends Activity implements OnClickListener {
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   Button sendButton = (Button) this.findViewById(R.id.btnsend);
   sendButton.setOnClickListener(this);
   }
   public void onClick(View v) {
   Intent sendsms = new Intent(Intent.ACTION_VIEW);
   sendsms.setType("vnd.android-dir/mms-sms");
   sendsms.putExtra("address", "+919930229995");
   sendsms.putExtra("sms_body","hey wassuppp ?????");
   startActivity(sendsms);
   }
   }

Programmatically sending an SMS
-------------------------------

If you dont want to use built-in applications to send SMS.You can use the Android API to programmatically send
SMS messages from your own applications and We have to use SmsManager for sending SMS programmatically.
In order to send SMS,you need to add the following permission to your AndroidManifest.xml file:


   <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

SmsManager
----------
SmsManager Manages SMS operations such as sending data, text, and pdu SMS messages.We can get this object by calling the static method SmsManager.getDefault().



   SmsManager sms = SmsManager.getDefault();

SmsManager.getDefault() will get the default instance of SmsManager.


   SmsManager sms = SmsManager.getDefault();
   sms.DataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent

Arguements:

destinationAddress = is the phone number to which you want to send the SMS.
scAddress= is the service center address or null to use the current default SMSC.
destinationPort = the port to deliver the message to.
data =  is the body of the message to send.
sentIntent = if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed.
The result code will be Activity.RESULT_OK for success,RESULT_ERROR_GENERIC_FAILURE for Generic failure,
RESULT_ERROR_RADIO_OFF for radio off,RESULT_ERROR_NULL_PDU for null pdu
For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful    for troubleshooting.
The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.


   SmsManager sms = SmsManager.getDefault();
   sms.sendTextMessage("+9912345678", null, "hey wassup bro ?", null, null);

Example:




   import android.app.Activity;
   import android.os.Bundle;
   import android.telephony.SmsManager;
   import android.view.View;
   import android.widget.Button;

   public class sendsms extends Activity {
    /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button send =(Button) findViewById(R.id.btnsend);
        send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("+9912345678", null, "Hi  how r u",null, null);

}
});
    }
    }

Comments

Post a Comment

Popular posts from this blog

Webview data directory for Android 9 (Pie)

Tabs in Android

DatePicker in Android