0

I' writing a code to show a notification channel in my application, in android studio. What I'm dealing with is that the android studio does not show me the notification bar in the application. What should I do to fix this issue?

package com.example.notification_example;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {


NotificationManager notification;
Button btn;
String ChannelID = "ChannelID";
String ChannelName = "ChannelName";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = findViewById(R.id.button);
    notification = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(ChannelID,
                        ChannelName,
                        NotificationManager.IMPORTANCE_DEFAULT);
                notification.createNotificationChannel(notificationChannel);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, ChannelID);

                builder.setSmallIcon(R.drawable.hand_notification)
                        .setContentTitle("Updates").setContentText("Updates Are ready to install");
                notification.notify(1, builder.build());
            }
        }
    });
}

}

When I click the button, the notification bar will not be indicated. enter image description here

MMd.NrC
  • 91
  • 7

1 Answers1

0

This is working fine for me. I added else to your code because I am using bluestacks (API 25). Note that the correct way to call system service is ContextCompat.getSystemService. See https://stackoverflow.com/a/61709171/6576302 or Cannot find symbol NOTIFICATION_SERVICE?

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {


    NotificationManager notification;
    Button btn;
    String ChannelID = "ChannelID";
    String ChannelName = "ChannelName";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.button);
        notification = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        btn.setOnClickListener(view -> {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(ChannelID,
                        ChannelName,
                        NotificationManager.IMPORTANCE_DEFAULT);
                notification.createNotificationChannel(notificationChannel);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, ChannelID);

                builder.setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("Updates").setContentText("Updates Are ready to install");
                notification.notify(1, builder.build());
            } else {
                Notification notification = new NotificationCompat.Builder(this, ChannelID)
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setContentTitle("Updates")
                        .setContentText("Updates Are ready to install")
                        .setAutoCancel(true)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .build();

                NotificationManager notificationManager = ContextCompat.getSystemService(this, NotificationManager.class);
                if (notificationManager != null) {
                    notificationManager.notify(1, notification);
                }
            }
        });
    }
}

enter image description here

C.F.G
  • 817
  • 8
  • 16