Having Problem showing the notification icon and its corresponding text and title in Android Studio

147 Views Asked by At

I have written the following code to show a notification in Android Studio:

package com.example.notificationexample;

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 {

Button btn;
NotificationManager notificationManager;
String channelID = "ID";
String channelName = "Name";

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

    btn = findViewById(R.id.notifybtn);
    notificationManager = (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 channel = new NotificationChannel(channelID,channelName,
                        NotificationManager.IMPORTANCE_DEFAULT);

                notificationManager.createNotificationChannel(channel);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, channelID )
                        .setSmallIcon(R.drawable.notification_icon).setContentTitle("Updating...")
                        .setContentText("New updates are ready to install");

                notificationManager.notify(1, builder.build());
                  }
        }
    });
}
}

The issue I'm dealing with is that Android Studio does not show me the notification icon. More importantly, it does not apply the action of setOnClickListenerwhen I press the button. What does the problem originate from?

enter image description here

1

There are 1 best solutions below

0
Stone On

My solution to in that use icon with transparent background. And this issue helped me https://github.com/expo/expo/issues/24844