Issue with Notification Manager on android

12,727

Solution 1

1. The constructor was deprecated in api level 11. so you should use Notification.Builder.

for e.g.

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

2. in your code you are passing the intent instead of pending in setLatestEventInfo

....
Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
        notify.setLatestEventInfo(context, title, details, pending);
        ns.notify(0,notify);
....

Solution 2

notificationManager =
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
   myNotification = new Notification(R.drawable.icon,
     "Notification!",
     System.currentTimeMillis());
   Context context = getApplicationContext();
   String notificationTitle = "Exercise of Notification!";
   String notificationText = "http://niravranpara.blogspot.com/";
   Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
   PendingIntent pendingIntent
     = PendingIntent.getActivity(AndroidNotification.this,
       0, myIntent,
       Intent.FLAG_ACTIVITY_NEW_TASK);
   myNotification.defaults |= Notification.DEFAULT_SOUND;
   myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
   myNotification.setLatestEventInfo(context,
      notificationTitle,
      notificationText,
      pendingIntent);
   notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

  }

Solution 3

Those constructor and method has been deprecated.so you should use 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();

Share:
12,727
Sai
Author by

Sai

Six years of tech industry experience, an expert in Android application development and experience as a software solutions architect. Published 40+ app to PlayStore.

Updated on June 04, 2022

Comments

  • Sai
    Sai almost 2 years

    I'm trying to notify using a button, but both Notification and setLatestEventInfo is deprecated.

    Two errors:

    1.The constructor Notification(int, CharSequence, long) is deprecated Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());

    2.The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) in the type Notification is not applicable for the arguments (Context, CharSequence, CharSequence, Intent) notify.setLatestEventInfo(context, title, details, intent);

    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {   
            @Override
            public void onClick(View v) {
                NotificationManager ns = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());
                Context context = MainActivity.this;
                CharSequence title ="you have be notified";
                CharSequence details = "Continue your work";
                Intent intent = new Intent(context,MainActivity.class);
                PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
                notify.setLatestEventInfo(context, title, details, intent);
                ns.notify(0,notify);
    
    
            }
        });
    }
    

    API LEVELS:

           android:minSdkVersion="11"
           android:targetSdkVersion="17"
    

    What is the alternative?