Thursday, September 19, 2013

Create A Notification In Android

             Here am going to describe how a notification can make on the Android. A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

         For creating a notification, we need two android activities. One is the activity that is creating the Notification and second activity for managing the notification if the user selects the notification from the notification drawer .Every notification containing a unique id which represents the notification, here i used the notification id as 1.  
        
         Here am going to explain this by making a Android Application for displaying a simple Notification.
The name of the Application is "Notification".



      Here shows the android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notification"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notification.CreateNotificationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.notification.NotificationReceiverActivity"
            android:label="@string/title_activity_notification_receiver" >
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.NOTIFICATION" >
    </uses-permission>
</manifest>
The notification can be embedded with a notification icon, here i used a small png image for displaying the notification icon..


    Here is the android layout for the activity CreateNotificationActivity.java( main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click on the button below to see the Status Bar Notification"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Status Bar Notification"
    android:id="@+id/statusbarbutton"
    />
</LinearLayout>



Here shows the Android activity for creating the notification(CreateNotificationActivity.java)
package com.example.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CreateNotificationActivity extends Activity {  
    private static final int NOTIFICATION_ID = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /** Define configuration for our notification */
        int icon = R.drawable.icon;
        CharSequence tickerText = "Hi Am ready";
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = "Sample notification";
        CharSequence contentText = "Touch me to open";
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();
        } catch (Exception e) {}
        Intent notificationIntent = new Intent(this, NotificationReceiverActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
         
        /** Initialize the Notification using the above configuration */
        final Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
 
        /** Retrieve reference from NotificationManager */
        String ns = Context.NOTIFICATION_SERVICE;
        final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
         
        /** Listener for click event of the button */
        Button statusbarnotify = (Button) findViewById(R.id.statusbarbutton);
        statusbarnotify.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {              
                mNotificationManager.notify(NOTIFICATION_ID, notification);
            }
        });  
    }   
}


This code creates the Notification when clicking on the button.. we have to manage what will happened when the user selected the activity from the notification bar, for that we are creating an another android activity which is executed when user selects that notification.
 
Here is the code for that activity(NotificationReceiverActivity.java)
package com.example.notification;

import android.app.Activity;
import android.os.Bundle;

public class NotificationReceiverActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
  }
} 
And here is the Layout file(result.xml)
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_notification"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >
    <TextView
        android:id="@+id/notification_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"                  
    />
    <TextView
        android:id="@+id/notification_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/notification_image"
        android:layout_below="@+id/notification_title"
    />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/notification_title"
        android:layout_centerHorizontal="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/notification_title"
        android:layout_centerHorizontal="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/notification_text"
        android:layout_marginTop="55dp"
        android:layout_toRightOf="@+id/notification_text"
        android:text="@string/i"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

No comments:

Post a Comment