Home > Enterprise >  Why is my code not displaying notifications
Why is my code not displaying notifications

Time:12-29

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.

CodePudding user response:

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);
        }

CodePudding user response:

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.

CodePudding user response:

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)
    }
}
  • Related