Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 286
Saariko Avatar asked Jun 17 '11 21:06

Saariko


People also ask

How do I use NotificationCompat?

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.

What is notification builder?

android.app.Notification.Builder. Builder class for Notification objects. Provides a convenient way to set the various fields of a Notification and generate content views using the platform's notification layout template.

What is NotificationCompat in Android?

NotificationCompat.Action. Structure to encapsulate a named action that can be shown as part of this notification. NotificationCompat.Action.Builder. Builder class for Action objects.


1 Answers

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); 
like image 65
Rabi Avatar answered Oct 13 '22 07:10

Rabi