lekhlifi
lekhlifi

Reputation: 47

create android notification by click in a button

I try to create android notification, when i click in a button but does not work. this is the code that i write in button

NotificationManager Nm;
    public void Notify(View view) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setContentTitle("LOGIN")
                .setContentText("You are login successfully")
                .setSmallIcon(R.drawable.done);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, mBuilder.build());
    }

Upvotes: 1

Views: 974

Answers (1)

jle
jle

Reputation: 696

As already stated by Ikazuchi, for android versions since android 8 you'll need to add a notification channel. This can be done, as it's shown in the documentation here: https://developer.android.com/training/notify-user/build-notification#java, 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: 2

Related Questions