Search code examples
androidnotificationsdeprecatedsuppress-warnings

In Android, how do I remove a notification variable from being deprecated?


How do I remove the deprecation from my code? I seem to be getting warnings because of this. Strike-through texts were "new Notification" and setLatestEventInfo? It says "This method is deprecated in API level 11? What does that mean? Pleas help. How do I solve this?

@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "OnStartCommand()", Toast.LENGTH_SHORT).show();
    NotificationManager notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationintent = new Intent(this, Reminder_2.class);
    PendingIntent pendingintent = PendingIntent.getActivity(this, 0, notificationintent, 0);
    int icon=R.drawable.ic_launcher;
    long when=System.currentTimeMillis();
    @SuppressWarnings("deprecation")
    Notification notification=new Notification (icon, "There's a message", when);
    notification.setLatestEventInfo(this, "Title here", "Content here.", pendingintent);
    notificationmanager.notify(033, notification);
    return super.onStartCommand(intent, flags, startId);
}

Solution

  • It's been deprecated, which means it has been flagged for removal in future versions of Android, or that a better standard/alternative has been introduced. The documentation recommends using Notification.Builder instead:

    Notification noti = new Notification.Builder(mContext)
             .setContentTitle("New mail from " + sender.toString())
             .setContentText(subject)
             .setSmallIcon(R.drawable.new_mail)
             .setLargeIcon(aBitmap)
             .build();
    

    Take a look at the documentation: http://developer.android.com/reference/android/app/Notification.Builder.html