Android notifications: compatibility with APIs

Faraz Azhar picture Faraz Azhar · Oct 16, 2012 · Viewed 8.1k times · Source

I'm trying to show notifications in my app along with a progress bar in it.

My app specs are that I'm targeting API 15 with minimum SDK set to 8. I'm using the following code to show notifications:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

mBuilder.setContentTitle("My Application")
        .setContentText("Downloading...")
        .setProgress(0, 0, true)
        .setSmallIcon(R.drawable.ic_launcher);

PendingIntent in = PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0);
mBuilder.setContentIntent(in);

mNotificationManager.notify(0, mBuilder.build());

With the above code the notifications appear in the notification drawer but no progress bar (on Android 2.2, and 2.3.3). But the progress bar appears fine on Android 4.1. So its obvious that its a compatibility issue.

How do I code that my notification with an indefinite progress bar appear on the APIs that I'm targeting. I tried using Notification.Builder but this class is not available on my current specs.

Thanks in advance!

-Faraz Azhar

Answer

Teovald picture Teovald · Oct 16, 2012

I encourage you to use Notification Compat 2. It has been created to handle this kind of problem.
If you want to use a custom RemoteViews for your notifications, have a look at my fork. I have made a pull request but it has not been integrated yet.