epilif3sotnas
epilif3sotnas

Reputation: 84

My notification doesn't appear when I click in my button

With this code, I want to click in a button and appear a notification. The problem is when in click in my button doesn't appear any notification. I search in youtube, in example codes and I don't see my error, can you see??


btNotify.setOnClickListener(new View.OnClickListener() {
@Override
    public void onClick(View v) {
        notification();
    }
});


public void notification() {
        NotificationCompat.Builder builder = new 
        NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_baseline_access_time_24);
        builder.setContentTitle("Time");
        builder.setContentText("Isto é um relógio");

        NotificationManager notifManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);
        notifManager.notify(1, builder.build());
}

Upvotes: 0

Views: 205

Answers (1)

cewaphi
cewaphi

Reputation: 410

You didn't mention the android version you are working with. According to the official documentation you have to provide a channel to the builder constructor when using API level 26+ (Android 8 or higher).

var builder = NotificationCompat.Builder(this, CHANNEL_ID)

It is recommended to create the channel right on start-up of the app since you can not post notifications without it (again, this is valid for API level 26+). Don't forget to assign a priority to the channel. You can find the details about creating a channel here.

Anyways, notifications are usually used for providing information to the user while the app is NOT in use. If you just want an instant notification following a button press, could you also consider using a Toast or a snackbar? It can be as simple as:

Toast.makeText(yourContext, "Your message", Toast.LENGTH_LONG).show()

Update 2020/08/11

Update according to the android version you mentioned.

The android documentation states the following:

Notice that the NotificationChannel constructor requires an importance, using one of the constants from the NotificationManager class. This parameter determines how to interrupt the user for any notification that belongs to this channel—though you must also set the priority with setPriority() to support Android 7.1 and lower (as shown above).

  1. by using the NotificationCompat builder, you get upward compatibility with android 8+ by creating a channel and providing it to the NotificationCompat constructor. For lower versions the channel is simply ignored.
  2. A priority setting is strictly required for using notifications. For support with versions lower or equal to Android 7.1: you need to set the priority in the builder since the channel (which has a priority setting on its own) is ignored.

Try adding the required importance specification with setPriority in the builder:

val builder ...
    .setPriority(NotificationCompat.PRIORITY_DEFAULT) // or any other priority level 

Update 2020-08-17

How to create a Channel for API level 26+ as shown in the official documentation:

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

Upvotes: 1

Related Questions