Wednesday, May 30, 2012

Generate Notification using Notification.Builder for Android 3.0

Android 3.0 (API Level 11) introduce Notification.Builder, allows easier control over all the flags, as well as help constructing the typical notification layouts.

It's a example of using Notification.Builder, modified from the previous exercise "schedule a repeating alarm".

Generate Notification using Notification.Builder for Android 3.0


Change the code of AlarmReceiver.java in "schedule a repeating alarm", to generate Notificaton.

package com.exercise.AndroidTime;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent arg1) {
  Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
  buildNotification(context);
  
 }
 
 private void buildNotification(Context context){
  NotificationManager notificationManager 
  = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
  Notification.Builder builder = new Notification.Builder(context);
  
  Intent intent = new Intent(context, AndroidTimeActivity.class);
  PendingIntent pendingIntent 
  = PendingIntent.getActivity(context, 0, intent, 0);
  
  builder
  .setSmallIcon(R.drawable.ic_launcher)
  .setContentTitle("ContentTitle")
  .setContentText("ContentText")
  .setContentInfo("ContentInfo")
  .setTicker("Ticker")
  .setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs)
  .setContentIntent(pendingIntent)
  .setAutoCancel(true);
  
  Notification notification = builder.getNotification();
  
  notificationManager.notify(R.drawable.ic_launcher, notification);
 }

}


Please notice that you have to change your project to target API Level 11, in order to use Notification.Builder.

Download the files.


5 comments:

Unknown said...

Very useful guide, worked like a charm!

Quick question, if I wanted to put an Extra to the intent, would the PendingIntent structure change?

Note: "incoming body" refers to the "OriginatingAdress" and is of the type String.

Code here:

private void buildNotification(Context context){
NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);

Intent intent = new Intent(context, SMSComposer.class);
intent.putExtra("MESSAGE", incomingBody);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

builder
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(incomingBody)
.setTicker(ticker + messageType)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

Notification notification = builder.getNotification();

notificationManager.notify(R.drawable.ic_launcher, notification);

}

Erik said...

Hello Daniel Alvarado,

In my understanding, the PendingIntent structure is same.

Unknown said...

i have problem with this code
Notification nNotify = new Notification.Builder(this)
the Builder always in red colour
can u help me..
i'm using the android studio

Aquiles said...

Hello sir,
i would like to ask how do i input multiple alarm into the codes using pending intent.
Your tutorials have been most helpful.

Unknown said...

This blog always make my project more easy, keep writing, your blog most helpful.