Skip to content Skip to sidebar Skip to footer

Notificationcompat.builder: Cannot Resolve Method Build()

I have the following simple class that I would like to use to notify user of incoming messages (I will evolve it as the app goes). But for now, in the last line, there is the follo

Solution 1:

Notification method is changed a bit in support V4 and above API level 19 . You can try the below code block.

publicvoidsendNotification(String message, String title, Intent intent, int not_id) {
    PendingIntentpendingIntent= PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    UridefaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notification;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        notification
                = newNotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_icon)
                .setStyle(newNotificationCompat.BigTextStyle().bigText(message))
                .setContentTitle(title)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

    } else {
        Bitmapbitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon);
        notification
                = newNotificationCompat.Builder(this)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.small_icon)
                .setStyle(newNotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message)
                .setAutoCancel(true)
                //.setColor(Color.parseColor("#1a4994"))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setLargeIcon(bitmap)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    }
    NotificationManagernotificationManager=
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(not_id, notification.build());
}

Update for NotificationChannel :

publicvoidinitChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManagernotificationManager=
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannelchannel=newNotificationChannel("default",
                                                          "Channel name",
                                                          NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Channel description");
    notificationManager.createNotificationChannel(channel);
}

Solution 2:

privatevoidcreateNotification(){
    Intentintent=newIntent(this,NotificationReceive.class);
    PendingIntentpendingIntent= PendingIntent.getActivity(this,(int) System.currentTimeMillis(),intent,0);
    Notificationnotification=newNotification.Builder(this)
            .setContentTitle("Message");
        .setContentText("this is a tittle")
        .setSmallIcon(R.drawable.schollo)
        .setContentIntent(pendingIntent);
        .build();

    NotificationManagernotificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notification.flags = notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0,notification);
}

Post a Comment for "Notificationcompat.builder: Cannot Resolve Method Build()"