问题描述:在Android 9(API 28)上,通知无法显示。
解决方法:在Android 9(API 28)上,通知显示需要进行一些额外的设置。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "your_channel_id";
String channelName = "Your Channel Name";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
String channelId = "your_channel_id";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
请注意,上述代码中的"your_channel_id"需要替换为你自己的通知通道ID,R.drawable.notification_icon需要替换为你自己的通知图标。
通过以上步骤,你应该能够在Android 9(API 28)上成功显示通知了。