Thursday 13 October 2011

Default Notification

Setting the default notification to the notification bar.

For setting the default notification, we have to use the android Notification Manager, Notification, Pending Intent.

Notification Manager - For getting notification service and setting the notification.
Notification - For adding contents inside the notification.
Pending Intent- For calling the activity on clicking the notification.

In notification, first we have to instantiate the notification, that will describe which application notification you are receiving..

For Instantiating the notification, below code is used.


               CharSequence tickerText  = "Default Notificaion";
long when = System.currentTimeMillis();
int icon = R.drawable.notf_icon;
Notification notification = new Notification(icon, tickerText, when);


This code will display a small icon at left side followed by the notification text.

At the time of receiving notification, you can set the vibration or display light color.

For setting the vibration, Below code is used.and you have to add permission in the manifest file.


notification.defaults = Notification.DEFAULT_VIBRATE; 


this will set the default vibration.

                         (or)


You can also define the your own vibration pattern.

int dot = 200;      // Length of a Morse Code "dot" in milliseconds
int dash = 500;     // Length of a Morse Code "dash" in milliseconds
int short_gap = 200;    // Length of Gap Between dots/dashes
int medium_gap = 500;   // Length of Gap Between Letters
int long_gap = 1000;    // Length of Gap Between Words
long[] pattern = {
   0,  // Start immediately
   dot, short_gap, dot, short_gap, dot,    // s
   medium_gap,
   dash, short_gap, dash, short_gap, dash, // o
   medium_gap,
   dot, short_gap, dot, short_gap, dot,    // s
   long_gap
};
notification.vibrate=pattern;


For setting the LED light color, the below code is used. Setting LED light color is fully depends upon the Hardware support.


notification.defaults=Notification.DEFAULT_LIGHTS;


this will set the default LED light.
                               
                                  (or)
You can also define your owl light color by using the below code.

notification.ledARGB = 0xff0000ff;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;


The complete code for Default Notification with Vibration and the LED light display is as follows.



package com.sample.notification;

import java.util.Calendar;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;

public class Test extends Activity
{
private Button button1;
private static final int NOTIFICAION_ID = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button1 = (Button) findViewById(R.id.button01);
        button1.setOnClickListener(button1Listner);
        
        onPush("Notification 2","Channel 2");
    }
    
public void onPush(String notification, String channel) 
{
stausBarNotificaion(notification, channel); // For Default Notification
}
public void stausBarNotificaion(String message, String Channel)
{
String ns = NOTIFICATION_SERVICE;
NotificationManager notfManager = (NotificationManager) getSystemService(ns);
CharSequence tickerText  = "Default Notificaion";
long when = System.currentTimeMillis();
int icon = R.drawable.notf_icon;
Notification notification = new Notification(icon, tickerText, when);
Context context = this.getApplicationContext();
CharSequence contentTitle = "A.B.C.E.F.G.H | TESTING";
CharSequence contentText = message;
Intent notificaionIntent = new Intent("com.sample.notification.Notificaions");
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificaionIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//notification.defaults = Notification.DEFAULT_VIBRATE;
int dot = 200;      // Length of a Morse Code "dot" in milliseconds
int dash = 500;     // Length of a Morse Code "dash" in milliseconds
int short_gap = 200;    // Length of Gap Between dots/dashes
int medium_gap = 500;   // Length of Gap Between Letters
int long_gap = 1000;    // Length of Gap Between Words
long[] pattern = {
   0,  // Start immediately
   dot, short_gap, dot, short_gap, dot,    // s
   medium_gap,
   dash, short_gap, dash, short_gap, dash, // o
   medium_gap,
   dot, short_gap, dot, short_gap, dot,    // s
   long_gap
};
notification.vibrate=pattern;
//notification.defaults=Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff0000ff;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notfManager.notify(NOTIFICAION_ID,notification);
}
private OnClickListener button1Listner = new View.OnClickListener() {
public void onClick(View arg0)
{
startActivity(new Intent("com.sample.notification.Notificaions"));
}
};
}

Screen shot for the Results.





No comments:

Post a Comment