31

I have a small problem but dont understand how to get out of this.

I created a class for providing Notifications, but these lines are marked deprecated:

...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...

Alternative methods are:

...
Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build(); // available from API level 11 and onwards
...

Can i write a code something like:

if(API_level < 11)
{
...
    Notification notification = new Notification(icon, text, time); // deprecated in API level 11
    ...
    notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
    ...
}

else
{
    ...
    Notification noti = new Notification.Builder(mContext)
             .setContentTitle("New mail from " + sender.toString())
             .setContentText(subject)
             .setSmallIcon(R.drawable.new_mail)
             .setLargeIcon(aBitmap)
             .build(); // available from API level 11 and onwards
    ...
}

I providing the minimum sdk version as "8".

Edit:

I did like below:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){

            Notification notification = new Notification(icon, text, time);

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);

            notification.setLatestEventInfo(this, title, text, contentIntent);

            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            mNM.notify(NOTIFICATION, notification);
        } 
        else
        {
            // what to write here
        }

What can i write for else portion ??

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174

2 Answers2

76

This is how i ended up to the solution:

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {

            notification = new Notification(icon, text, time);
            notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(text).setWhen(time)
                    .setAutoCancel(true).setContentTitle(title)
                    .setContentText(text).build();

            mNM.notify(NOTIFICATION, notification);
        }

Edit:

The above solution works. Still, since, NotificationCompat.Builder class was introduced, we can skip the if condition for checking that compares current API version. So, we can simply remove the if...else condition, and go with:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
                        this);
notification = builder.setContentIntent(contentIntent)
                      .setSmallIcon(icon).setTicker(text).setWhen(time)
                      .setAutoCancel(true).setContentTitle(title)
                      .setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • I used `NotificationCompact.Builder`. – Chintan Soni May 31 '13 at 14:01
  • 15
    FYI `setLatestEventInfo()` was removed from the SDK in Android 6.0. [source](http://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html#android.app.Notification.setLatestEventInfo_removed%28android.content.Context,%20java.lang.CharSequence,%20java.lang.CharSequence,%20android.app.PendingIntent%29) – rekire Sep 24 '15 at 08:44
  • time should be System.currentTimeMillis() if you want the current time. http://stackoverflow.com/a/27721837/211457 – Andy Jan 16 '16 at 07:18
  • where i can found NotificationCompat.Builder? how i can include it in my project? EDIT: its on android.support.v4 – CDrosos Jan 08 '17 at 17:54
6

This is the correct way to get the level.

 final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);

if (sdkVersion < Build.VERSION_CODES.HONEYCOMB)
{
...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
}
else
{
Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build(); // available from API level 11 and onwards
} 

All the version codes can be found at this developer link.

Tarun
  • 13,727
  • 8
  • 42
  • 57
  • Heyy, i got errors in these lines you wrote in else portion, telling that `call requires API level 11 or more` – Chintan Soni May 31 '13 at 08:24
  • Build.VERSION_CODES.HONEYCOMB = 11 so else part will be called only if the api is greater than 11. – Tarun May 31 '13 at 09:24
  • Ya thats fine. But how to remove those errors ?? With this, i can't even run my project. Please give me some solution to this... – Chintan Soni May 31 '13 at 11:26
  • What is the error? Can you paste it here... Add your manifest uses-sdk also. – Tarun May 31 '13 at 12:36