更新 Android Studio 后未显示通知

Notification is not showing after updating Android Studio

我最近对 ​​Android Studio 进行了更新,之后就无法显示通知了。

代码在更新前最初工作正常,所以我相信问题出在软件上,而不是代码上。更新后我需要更改什么吗?

我的程序实际上会显示一个 Toast 消息,还有一个通知说一个帐户已经注册(注册帐户后)。然而,在吐司消息之后,另一条吐司消息显示,"Developer warning for package"com.example.jianminong.aucon“无法post通知通道"personal"查看日志了解更多详情”。

public void displayNotification(){
    NotificationCompat.Builder builder = new 
NotificationCompat.Builder(this,CHANNEL_ID);
    builder.setSmallIcon(R.drawable.aucon);
    builder.setContentTitle("Welcome " + 
editUsername.getText().toString());
    builder.setContentText("You have just created an account with 
the email of " +editEmail.getText().toString());
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat notificationManagerCompat = 
NotificationManagerCompat.from(this);

notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());
}


private void createNotificationChannel(){
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
    {
        CharSequence name = "Personal";
        String description = "HAHA";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

        notificationChannel.setDescription(description);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

如果您在较新的 android 设备上运行,则需要使用新的通知渠道:

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear.

您需要遵循以下步骤: https://developer.android.com/training/notify-user/channels

您是否在应创建通知渠道的代码 createNotificationChannel() 方法的某处调用?因为如果不是,则不会创建通道,因此不会创建 toast 消息。您可以在显示通知之前调用它:

public void displayNotification(){
    createNotificationChannel()
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
    ...
}