How exactly to use Notification.Builder
I found that I am using a deprecated method for noficitations (notification.setLatestEventInfo())
It says to use Notification.Builder.
- How do I use it?
When I try to create a new instance, it tells me:
Notification.Builder cannot be resolved to a type
Notification.Builder API 11 or NotificationCompat.Builder API 1
This is a usage example.
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
This is in API 11, so if you are developing for anything earlier than 3.0 you should continue to use the old API.
Update: the NotificationCompat.Builder class has been added to the Support Package so we can use this to support API level v4 and up:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
in addition to the selected answer here is some sample code for the NotificationCompat.Builder
class from Source Tricks :
// Add app running notification
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, builder.build());
}
// Remove notification
private void removeNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(FM_NOTIFICATION_ID);
}
Notification Builder is strictly for Android API Level 11 and above (Android 3.0 and up).
Hence, if you are not targeting Honeycomb tablets, you should not be using the Notification Builder but rather follow older notification creation methods like the following example.
UPDATE android-N (march-2016)
Please visit Notifications Updates link for more details.
- Direct Reply
- Bundled Notifications
- Custom Views
Android N also allows you to bundle similar notifications to appear as a single notification. To make this possible, Android N uses the existing
NotificationCompat.Builder.setGroup()
method. Users can expand each of the notifications, and perform actions such as reply and dismiss on each of the notifications, individually from the notification shade.This is a pre-existing sample which shows a simple service that sends notifications using NotificationCompat. Each unread conversation from a user is sent as a distinct notification.
This sample has been updated to take advantage of new notification features available in Android N.
sample code.