Livello di difficoltà: facileVersione SDK utilizzata: 1.6Link al file compresso del progetto eclipse: file in allegatoIn questo tutorial cercherò di spiegare come è possibile inviare una notifica alla Status Bar, ma prima un po' di teoria

La classe Notification mostra una notifica persistente all'utente tramite il NotificationManager.
Una Status Bar Notification aggiunge un'icona alla Status Bar di sistema (opzionalmente, accompagnata da un messaggio di testo a scomparsa) e un messaggio di testo esteso nella Notification window.
Quando l'utente seleziona il messaggio esteso, Android lancia un Intent che identifica la notifica (di norma lancia un'Activity).
È possibile personalizzare questa notifica aggiungendo suoni, vibrazione, lampeggio del led del dispositivo.
Un'Activity o un Servizio possono inizializzare la Status Bar Notification.
Poiché un'Activity per compiere delle azioni deve avere il focus, occorre delegare il lancio delle notifiche ad un servizio che opera in background, così da poterle inviare anche quando l'Activity principale non è direttamente utilizzata dall'utente (es: dispositivo in stato di sleep).
Per creare una notifica si devono utilizzare due classi: Notification e NotificationManager.
Si usa un'Istanza di Notification per definire le proprietà della status bar (icona, messaggio esteso e impostazioni extra).
Il NotificationManager è un servizio di sistema di Android che serve per eseguire e gestire le Notifications. È per questo necessario istanziare il Notification Manager.
Il collegamento tra Notification e NotificationManager viene implementato attraverso il metodo getSystemService() e, in seguito, quando occorre notificare qualcosa all'utente, si passa l'oggetto Notification tramite il metodo notify().
Ottieni un reference al NotificationManager:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);Inizializzazione del Notification:
int icon = android.R.drawable.stat_notify_chat;
CharSequence tickerText = "Questo è il tickerText";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);Definizione del messaggio esteso della notifica e del relativo Intent:
Context context = getApplicationContext();
CharSequence contentTitle = "Titolo della mia notifica";
CharSequence contentText = "Testo della mia notifica";
Intent notificationIntent = new Intent(this, TutorialNotification.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);Aggiungere alla notifica del suono di default, la vibrazione di default e il lampeggio del LED di default (opzionale):
notification.defaults |= Notification.DEFAULT_SOUND; //Suona
notification.defaults |= Notification.DEFAULT_LIGHTS; //LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //VibraPassaggio della Notification al NotificationManager:
private static final int NOTIFICATION_ID = 1;
mNotificationManager.notify(NOTIFICATION_ID, notification);Se si è aggiunta la notifica tramite vibrazione occorre inserire il relativo permesso nell'AndroidManifest.xml:
<uses-permission android:name="android.permission.VIBRATE" />E questo è tutto

Per maggiori dettagli (suoni, vibrazione, notifica led personalizzati, custom view) rimando alla
documentazione ufficiale.
Sorgenti:TutorialNotification.java
package it.anddev.tutorialnotification;
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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class TutorialNotification extends Activity implements OnClickListener {
private static final int NOTIFICATION_ID = 1;
NotificationManager mNotificationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Inizializzo i click listeners per tutti i pulsanti
View btnSendNotify = findViewById(R.id.btnSendNotify);
btnSendNotify.setOnClickListener(this);
View btnEraseNotify = findViewById(R.id.btnEraseNotify);
btnEraseNotify.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSendNotify:
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = android.R.drawable.stat_notify_chat;
CharSequence tickerText = "Questo è il tickerText";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Titolo della mia notifica";
CharSequence contentText = "Testo della mia notifica";
Intent notificationIntent = new Intent(this, TutorialNotification.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND; //Suona
notification.defaults |= Notification.DEFAULT_LIGHTS; //LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibra
mNotificationManager.notify(NOTIFICATION_ID, notification);
break;
case R.id.btnEraseNotify:
if(mNotificationManager != null){
mNotificationManager.cancel(NOTIFICATION_ID);
}
break;
}
}
}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"
android:gravity="center">
<Button
android:layout_height="wrap_content"
android:text="Invia Notifica"
android:id="@+id/btnSendNotify"
android:layout_width="128dip"></Button>
<Button
android:layout_height="wrap_content"
android:text="Elimina notifica"
android:id="@+id/btnEraseNotify"
android:layout_width="128dip"></Button>
</LinearLayout>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="it.anddev.tutorialnotification"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".TutorialNotification"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk
android:minSdkVersion="4" />
<uses-permission
android:name="android.permission.VIBRATE" />
</manifest>Bibliografia: