Skip to main content

Create Notification In Android

Create Notification Example In Android With New Virsion SDK 4.4(Kitkat)


1] First Make Code In Java File MainActivity.java:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private NotificationManager mNotificationManager;
    private int notificationID = 100;
    private int numMessages = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Button startBtn = (Button) findViewById(R.id.start);
          startBtn.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
                displayNotification();
             }
          });
          Button cancelBtn = (Button) findViewById(R.id.cancel);
          cancelBtn.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
                cancelNotification();
             }
          });
     
         /* Button updateBtn = (Button) findViewById(R.id.update);*/
         /* updateBtn.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
                updateNotification();
             }
          });*/
    }
    @SuppressLint("NewApi")
    protected void displayNotification() {
          /* Invoking the default notification service */
          NotificationCompat.Builder  mBuilder =
          new NotificationCompat.Builder(this);  

          mBuilder.setContentTitle("New Messag Arraived");
          mBuilder.setContentText("You Have Got New Message From YO YO Honey Singh.");
          mBuilder.setTicker("New Message Come!");
          mBuilder.setSmallIcon(R.drawable.a);

          /* Increase notification number every time a new notification arrives */
          mBuilder.setNumber(++numMessages);
        
          /* Creates an explicit intent for an Activity in your app */
          Intent resultIntent = new Intent(this, NotificationView.class);

          TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
          stackBuilder.addParentStack(NotificationView.class);

          /* Adds the Intent that starts the Activity to the top of the stack */
          stackBuilder.addNextIntent(resultIntent);
          PendingIntent resultPendingIntent =
             stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
             );

          mBuilder.setContentIntent(resultPendingIntent);

          mNotificationManager =
          (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

          /* notificationID allows you to update the notification later on. */
          mNotificationManager.notify(notificationID, mBuilder.build());
       }

       protected void cancelNotification() {
          Log.i("Cancel", "notification");
          mNotificationManager.cancel(notificationID);
       }
 }

2] Create Second NotificationView.java File:

package com.example.notificationdemo;
import android.app.Activity;
import android.os.Bundle;

public class NotificationView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);
}
}

3] Create main.xml File:

    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
<Button android:id="@+id/start"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Hit Button For Notification/>

   <Button
       android:id="@+id/cancel"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:layout_marginTop="96dp"
       android:text="Hit Button For Notification Clear" />

</RelativeLayout>

4] Create Second notification.xml File:

      <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
 <TextView
   android:layout_width="fill_parent"
   android:layout_height="400dp"
   android:text="Wel Come To YOYO HoneySingh House" />
</LinearLayout>


5] Output:

     


          Notification Arrived In Below Screen After Press First Button:
       






  

Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"