Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android java.lang.NoClassDefFoundError: android.app.Notification$Builder

i add my app to google play , but i get some crashes on notification bulider , this is the crash message

java.lang.NoClassDefFoundError: android.app.Notification$Builder

and this my notification create code ,

    public void CreateNtf(String text)
{   

    Notification notificationView;
    PendingIntent pendingIntent = PendingIntent.getActivity( MainActivity.this,0,getIntent(),Intent.FLAG_ACTIVITY_NEW_TASK);
    if(Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT <= 15){

         notificationView = new Notification.Builder(context)
        .setContentTitle(getResources().getString(R.string.app_name))
        .setContentText(text)
        //.setTicker(getResources().getString(R.string.app_name) + " " + text)
        .setWhen(System.currentTimeMillis())
        .setContentIntent(pendingIntent)
        //.setDefaults(Notification.DEFAULT_SOUND)
        .setAutoCancel(false)
        .setSmallIcon(R.drawable.ic_launcher)
        .getNotification();
    }else{
             notificationView = new Notification.Builder(context)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(text)
            //.setTicker(getResources().getString(R.string.app_name) + " " + text)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            //.setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(false)
            .setSmallIcon(R.drawable.ic_launcher)
            .build();

    }
    notificationView.flags |= Notification.FLAG_NO_CLEAR;
    notificationView.flags |= Notification.FLAG_ONGOING_EVENT;
  notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.notify(1, notificationView);




}

So how can i fix this problem please ? i still get errors and crashes in google play

like image 537
Bahaa Odeh Avatar asked Feb 18 '26 08:02

Bahaa Odeh


1 Answers

Notification.Builder is not supported on API lower than 11. You can use NotificationCompat.Builder or create notification directly and use setLatestEventInfo.

like image 51
TN. Avatar answered Feb 20 '26 20:02

TN.