I am trying to take the title of the notification from the user to show when they reach the highest value and I did this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
minTitle1 = (EditText) findViewById(R.id.minTitle1) ;
maxTitle1 = (EditText) findViewById(R.id.maxTitle1) ;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
highestText = Integer.parseInt(highest.getText().toString().trim());
minimumText = Integer.parseInt(minimum.getText().toString().trim());
minTitle =minTitle1.getText().toString();
maxTitle=maxTitle1.getText().toString();
} catch(Exception ex){
Toast.makeText(getApplicationContext(),"Please enter numbers!",Toast.LENGTH_SHORT).show();
}
}
});
countUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(highestText == 0 && minimumText == 0){
Toast.makeText(getApplicationContext(),"Please enter the target numbers!",Toast.LENGTH_SHORT).show();
}else {
if (highestText == count 1 ) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "number");
builder.setContentTitle(maxTitle);
builder.setContentText("please be aware that you reached the maximum number");
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
managerCompat.notify(1, builder.build());
}
For some reason the title does not show up with the notification. Am I missing something? How can I fix it?
CodePudding user response:
For android 8.0 and above you have to create a notification channel like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
CodePudding user response:
Something what is missing here is the creation of a channel
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel to createNotificationChannel()
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)
}
}
Because you must create the notification channel before posting any notifications on Android 8.0 and higher, you should execute this code as soon as your app starts. It's safe to call this repeatedly because creating an existing notification channel performs no operation.
More information here