Wednesday, 6 September 2017

Show Notification in Android

About Example:- Here is the basic example that "How to Show Notification   in Android Application By using Coding"

Basic Requirement:-
1. MainActivity.java  | activity_main.xml
Steps are:-
Step 1: Add a new project and open activity_main.xml and write following code:-
<?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"   
 android:gravity="center"    
 tools:context="com.example.supriyabharti.notificationexamp.MainActivity">

 <Button        
 android:id="@+id/btnShow"        
 android:layout_width="wrap_content"        
 android:layout_height="wrap_content"       
 android:layout_gravity="center"      
 android:text="Show Notification"/>
 <Button        
 android:id="@+id/btnCancel"       
 android:layout_width="wrap_content"      
 android:layout_height="wrap_content"      
 android:layout_gravity="center"        
 android:text="Cancel Notification"/>
</LinearLayout>


Step 2:- Open MainActivity.java class and write code like following:-

package com.example.supriyabharti.notificationexamp;    
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button btnShow,btnCancel;
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnShow=(Button)findViewById(R.id.btnShow);
        btnCancel=(Button)findViewById(R.id.btnCancel);
        btnShow.setOnClickListener(new View.OnClickListener() {   
      public void onClick(View view) {
                showNotification();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override    
        public void onClick(View view) {
                cancelNotification();
            }
        });
    }
    public void showNotification(){
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.logotic);
        Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse("http://androidhubb.blogspot.in/"));
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,0);
        builder.setContentIntent(pendingIntent);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon));
        builder.setContentTitle("Android Important News");
        builder.setContentText("Android hubb is a blog..");
        builder.setSubText("supriya");
        NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(1,builder.build());
    }
    public void cancelNotification(){
        String str= Context.NOTIFICATION_SERVICE;
        NotificationManager notificationManager=(NotificationManager)getSystemService(str);
        notificationManager.cancel(1);
    }
}
Output:-


No comments:

Post a Comment