0

So I am trying to create a notification at the top of the phone, but when I run my code it does nothing. Not even a single error. ??? What am I doing wrong here?

public void createNotification(Context ctx) {

        SharedPreferences settings = ctx.getApplicationContext().getSharedPreferences(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 0);
        String contactName = settings.getString("curName", String.valueOf(0));
        String contactEmail = settings.getString("contactEmail", String.valueOf(0));
        String contactNumber = settings.getString("curPhone", String.valueOf(0));
        String dueAmount = String.valueOf(settings.getInt("amountDue", 0));

        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
        builder.setSmallIcon(R.drawable.myanlogo);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        builder.setContentIntent(pendingIntent);
        builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher));
        builder.setContentTitle("LETTING YOU KNOW");
        builder.setContentText("Your notification content here.");

        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(1, builder.build());

    }

I have it inside a class like this:

class ContactRVAdapter extends RecyclerView.Adapter<ContactRVAdapter.ViewHolder>

And I am calling the function here:

 holder.textDueTomorrow.setVisibility(View.GONE);
        if (Integer.valueOf(getFirstDateNumber) == Integer.valueOf(getSecondDateNumber) - 1) {
            holder.textDueTomorrow.setVisibility(View.VISIBLE);

            createNotification(context);

        }

Some advice would be nice because I have been working at this for a while now.

  • Please include the android version (api level) you are testing with. From docs: "Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear." This can be done with the builder, `setChannelId`. – Computable Dec 29 '22 at 02:29
  • API level is at 33 and I didn't think about a channel, how would I do that, with the current code I have? – user20791491 Dec 29 '22 at 02:35
  • Here's an [Q/A](https://stackoverflow.com/q/63894439/17856705) with some code to create notification channel. You'll have to consider when/where you want to put the code - some put it in their `Application` class to execute once [discussed here](https://stackoverflow.com/questions/46294833/correct-way-to-create-notification-channels-from-android-o-api). – Computable Dec 29 '22 at 02:43

3 Answers3

0

It looks like you're missing Notification Channels for Build.VERSION_CODE.O and greater https://developer.android.com/develop/ui/views/notifications/channels




        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
            notificationManager.createNotificationChannel(notificationChannel);

            // Make notification show big text.
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.setBigContentTitle(title);
            bigTextStyle.bigText(notificationText);
            // Set big text style.
            builder.setStyle(bigTextStyle);
        } else {
            builder.setContentTitle(title);
            builder.setContentText(notificationText);
        }

Javier Refuerzo
  • 274
  • 3
  • 7
0

This Chanel link below hellped me out along with the example provided below.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
            notificationManager.createNotificationChannel(notificationChannel);

            // Make notification show big text.
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.setBigContentTitle(title);
            bigTextStyle.bigText(notificationText);
            // Set big text style.
            builder.setStyle(bigTextStyle);
        } else {
            builder.setContentTitle(title);
            builder.setContentText(notificationText);
        }

This link makess it more clear. notification channel is not sending notifications Its basically 100% fine he just forgot to call function createNotificationChannel() before creating the notification.

0

For Android 8 and higher versions, you need to create a notification channel, write before your notification builder and pass same channel id to NotificationCompat.Builder

Copy/Paste this:

public void createNotification(Context ctx) {
    createNotificationChannel();

    SharedPreferences settings = ctx.getApplicationContext().getSharedPreferences(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 0);
    String contactName = settings.getString("curName", String.valueOf(0));
    String contactEmail = settings.getString("contactEmail", String.valueOf(0));
    String contactNumber = settings.getString("curPhone", String.valueOf(0));
    String dueAmount = String.valueOf(settings.getInt("amountDue", 0));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, "CHANNEL_ID");
    builder.setSmallIcon(R.drawable.myanlogo);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("LETTING YOU KNOW");
    builder.setContentText("Your notification content here.");

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

I have update this line NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, "CHANNEL_ID");

JAVA

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel("CHANNEL_ID", "Updates", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

KOTLIN

private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val notificationChannel = NotificationChannel("CHANNEL_ID", "Updates", NotificationManager.IMPORTANCE_DEFAULT)
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(notificationChannel)
    }
}
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23