How Push Notification works in android
In this blog we will read how push Notification works in Android . Notification is just like a message that you can show outside of your app.You can expand notification by clicking onto it.And If user do not want to get notification he can also disable notification for each application. you can also read overview from android Notification
activity_main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notification" android:id="@+id/text" android:textSize="30dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title_et" android:hint="Name" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/subject_et" android:hint="Subject" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/body_et" android:hint="Body" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Notification" android:id="@+id/button" /> </LinearLayout> |
In xml we take 3 edit Text . we put title in title_et , subject in subject_et and body of notification in body_et.
NotificationManager
Android provide Notification Manager class for sending notification purpose and You can initiate this class by getSystemService() method.
1 |
NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
Notification.Builder
The Builder provides an builder interface to create Notification object.You can set Notification Title , subject ,body and icon.You must enter icon to notification object . if you will not provide icon then It will crash so you need to provide notification icon.
Set Android Notification Properties
The properties of Android notification are set using Notification.Builder object. Some of the notification properties are mention below:
- setContentTitle(): It is used to set the title of notification.If you want to set title of notification so you can use set content title by using this method
- setContentText(): It is used to set the text message.If you want to set text to notification so you can set context text by using this method.
- setSmallIcon(): It sets the icon of notification. It is used to set icon to your notification so you can set icon by using this method.
- setPriority(): It sets the priority of notification.It is used to set priority to your notification so you can use this method to set priority.
1 2 3 |
Notification builder=new Notification.Builder (getApplicationContext()).setContentTitle(title.getText().toString()).setContentText(body.getText().toString()). setContentTitle(subject.getText().toString()).setSmallIcon(R.drawable.ic_status).build(); |
Notify
And at the last we need to notify to Notification Manager and notification will sent.
1 |
manager.notify(0,builder); |
MainActivity
In Main Activity you can see code for Notification so we provide here this Activity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class MainActivity extends AppCompatActivity { EditText title,subject,body; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); title=(EditText)findViewById(R.id.title_et); subject=(EditText)findViewById(R.id.subject_et); body=(EditText)findViewById(R.id.body_et); Button b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); createNotificationChannels(); Notification builder=new Notification.Builder (getApplicationContext()).setContentTitle(title.getText().toString()).setContentText(body.getText().toString()). setContentTitle(subject.getText().toString()).setSmallIcon(R.drawable.ic_status).build(); builder.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(0,builder); } }); } } |
Let us run application and test for Notification.We have added image please check notification
You can drag to bottom and check notification . please check image for notifcation.
And thanks for reading this blog, You can get other blogs from here