Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What "channelId" should I pass to the constructor of NotificationCompat.Builder?

I am trying to write a simple notification application. All it does is notify a notication whenever the button is clicked.

I write my app step by step according to this tutorial.

I don't understand what channel id I should pass to the constructor in order that the app will work. Now the app doesn't do anything (it's not collapses either). I guess that the problem is in the initialization of the variable NotificationCompat.Builder notifyBuilder. I didn't know what to pass so I just passed an arbitary string "0".

My questions are what should I pass to the constructor of NotificationCompat.Builder and will it make the app to work?

Here's the MainActivity:

public class MainActivity extends AppCompatActivity {
    private Button mNotifyButton;
    private static final int NOTIFICATION_ID = 0;
    private static final String NOTIFICATION_ID_STRING = "0";
    private NotificationManager mNotifyManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNotifyButton = (Button) findViewById(R.id.notificationButton);
    }

    public void sendNotification(View view) {

        mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder notifyBuilder
                =new NotificationCompat.Builder(this, NOTIFICATION_ID_STRING)
                .setContentTitle("You've been notified!")
                .setContentText("This is your notification text.")
                .setSmallIcon(R.drawable.ic_android);
        Notification myNotification = notifyBuilder.build();
        mNotifyManager.notify(NOTIFICATION_ID, myNotification);

    }
}
like image 682
J. Doe Avatar asked Sep 14 '25 09:09

J. Doe


1 Answers

private static final int NOTIFICATION_ID = 0;

This one identifies your notification. If you send multiple notifications with the same ID, the old notification will be replaced by the newer one. So, if want to display several notifications at once, you need multiple IDs.

private static final String NOTIFICATION_ID_STRING = "0";

This one is the ID of the channel. It tells which channel should be used to send that notification. You can assign any String. You just need to ensure that you use the same String for a specific channel. And if you want to create more channels, you need to use a different String for them.

About channel

In recent versions of Android (Android Oreo onwards), you can have different channels to send Notifications. This way, you allow your user to customize which notification he wants to receive.

For example, you have a shopping app. Then, you can have some channels such as:

  • My Orders (to notify about the orders)
  • Promotions (to notify about promotions.. this one, the user will probably want to disable).
  • Miscellaneous (to any other type of notification).

Before sending a notification on that channel, you should create it:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Orders Notification";
    String description = "Notifications about my order";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_STRING, name, importance);
    channel.setDescription(description);

    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

Then, you can send notifications via:

NotificationCompat.Builder notifyBuilder
            = new NotificationCompat.Builder(this, name.toString())

You can check all channes an app have via:

Settings -> Apps -> App you want to check -> Notifications

Some apps have just one channel. Other apps may have more than one channel.

Returning to your question

So, returning to you example, NOTIFICATION_ID_STRING can be any string. You just need to use "0" everytime you want to send a message on that specific channel. And, if you create a new channel, use a new string "channel1", for example.

However, you must create a channel before sending a notification on it:

// This ID can be the value you want.
private static final int NOTIFICATION_ID = 0;

// This ID can be the value you want.
private static final String NOTIFICATION_ID_STRING = "My Notifications";

public void sendNotification(View view) {

    mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //Create the channel. Android will automatically check if the channel already exists
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_STRING, "My Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("My notification channel description");
        mNotifyManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder notifyBuilder
            = new NotificationCompat.Builder(this, NOTIFICATION_ID_STRING)
            .setContentTitle("You've been notified!")
            .setContentText("This is your notification text.")
            .setSmallIcon(R.drawable.ic_android);
    Notification myNotification = notifyBuilder.build();
    mNotifyManager.notify(NOTIFICATION_ID, myNotification);
}

You can find more information about them HERE

like image 129
W0rmH0le Avatar answered Sep 16 '25 01:09

W0rmH0le