Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement the deprecated methods of Notification

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 ??

like image 581
Chintan Soni Avatar asked May 31 '13 07:05

Chintan Soni


1 Answers

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); 
like image 54
Chintan Soni Avatar answered Oct 06 '22 13:10

Chintan Soni