Kousalya
Kousalya

Reputation: 760

Not displaying Notification in android

I am trying to display notifications. But notification is not displayed. Below is my code,

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createNotificationButton = findViewById(R.id.button_create_notification);

        // Waits for you to click the button
        createNotificationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Starts the function below
                addNotification();
            }
        });
    }
    // Creates and displays a notification
    private void addNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel = new NotificationChannel("1" , "Notify", NotificationManager.IMPORTANCE_HIGH);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);


            NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
                    .setContentTitle("Test Title")
                    .setContentText("Test Message")
                    .setSmallIcon(R.mipmap.ic_launcher);

            notificationManager.notify(1, notification.build());



        }

    }

    }



I want to display a notification in top bar and want to clear or open it in my app...Anybody help me with this. Thanks in advance.

Upvotes: 0

Views: 519

Answers (1)

Susheel Karam
Susheel Karam

Reputation: 917

You should wrap only channel creation logic inside version check. Otherwise it won't show notification if the device OS version is less than Android O.

And You are using channel id as "1" while creating channel and "channel_id" while creating notification.

You should use same channelId string to create NotificationChannel and Notification.

Try this code:

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createNotificationButton = findViewById(R.id.button_create_notification);

        // Waits for you to click the button
        createNotificationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Starts the function below
                addNotification();
            }
        });
    }
    // Creates and displays a notification
    private void addNotification() {
        String channelId = "myNotificationChannel"; // Store channel ID as String or String resource
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel = new NotificationChannel(channelId , "Notify", NotificationManager.IMPORTANCE_HIGH);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
           }

            NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId) // Use  the same channelId String while creating notification
                    .setContentTitle("Test Title")
                    .setContentText("Test Message")
                    .setSmallIcon(R.mipmap.ic_launcher);

            notificationManager.notify(1, notification.build());


    }

}

Hope it helps

Upvotes: 2

Related Questions