antal1208
antal1208

Reputation: 85

Android Notification does not do anything

I want a notification on button press, the button press works fine because the toast "ITS OKAY!" is showing, but the notification does not appear. I just found a warning in logcat when the button is pressed:

W/libEGL: EGLNativeWindowType 0x75cc082010 disconnect failed

I know I must use CHANNEL_ID, but it's not better.

public void sendNotification() {
    Toast.makeText(this, "ITS OKAY!", Toast.LENGTH_SHORT).show();
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this, "CHANNEL_ID")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("It is my notification's Title!")
            .setContentText("Notification Body!");
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, mBuilder.build());
}

I want a notification but my code only shows the toast

Upvotes: 1

Views: 128

Answers (1)

jle
jle

Reputation: 696

As it was said you have to create a notification channel for versions newer than Android O. As it is said in the docs, you can do it like this:

 createNotificationChannel();
 Notification notification = new NotificationCompat.Builder(this, "channelID")
              .setSmallIcon(R.drawable.notification_icon)
              .setContentTitle("My notification")
              .setContentText("Much longer text that cannot fit one line...")
              .build();


 private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel serviceChannel = new NotificationChannel(
                "channelID",
                "Channel Name",
                importance
        );

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(serviceChannel);
    }
}

Upvotes: 3

Related Questions