Android 中未显示通知标题

Notification title not being shown in Android

我正在尝试获取用户通知的标题以显示他们何时达到最高值,我这样做了:

 @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());
                }

由于某种原因,该标题未随通知一起显示。我错过了什么吗?我该如何解决?

对于 android 8.0 及更高版本,您必须像这样创建一个通知渠道:

 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);
}

从您的代码可以清楚地看出,当您的最高文本等于 count+1 时,您的通知会弹出,但您正在更新提交按钮中的 maxtitle 值,因此当您收到通知时,它不会设置为您当前的 maxtitle 值是 shown.So 添加 maxTitle=maxTitle1.getText().toString();在你之前 builder.setContentTitle(最大标题);在您的 countUp 点击上更新您的最大标题。

         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");
                    maxTitle=maxTitle1.getText().toString();
                    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());
                }
        

这里缺少的是创建频道

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.

更多信息here