Wednesday, September 25, 2013

Notification Handling


In this tutorial I will explain how to add notifications to your android project

1. Create a project name “notifications”
2. Create a class “Notification Handling” and layout “home”
3. Add below code to view notices button 

Notification Handling.java


 String ns=Context.NOTIFICATION_SERVICE;
 NotificationManager snortificationManager=(NotificationManager) getSystemService(ns);
 int icon=R.drawable.mynotify;
 CharSequence tickerText="Drag Here To View Notices";
 long when=System.currentTimeMillis();
 Context context=getApplicationContext();
 CharSequence contentTitle="New Notifications";
 CharSequence contentText="You Have Received New Notices";
 Intent notifyIntent= new Intent(Home.this,ViewCourses.class);
 PendingIntent contentIntent =PendingIntent.getActivity(Home.this, 0, notifyIntent, 0);
 Notification notification=new Notification(icon,tickerText,when);   notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
 snortificationManager.notify(10,notification);

home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/backgrounddd"
 android:orientation="vertical" >

 <Button
 android:id="@+id/imageButtonn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_alignParentRight="true"
 android:layout_below="@+id/textView5"
 android:background="@drawable/button_shape_2"
 android:minHeight="30dp"
 android:text="View Notices" />

4. Import below classes to your Notification_handling.java

Import import android.app.Notification;
import android.app.NotificationManager;

5. Run the Application click "View Notices" button



That's All  :)
Thank You

4 comments:

Blogger Widgets