如果你在Android Studio中实现了通知功能,但是无法正常显示通知,可能是因为未添加通知渠道。以下是如何添加通知渠道的示例代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel("1", "Example Channel", NotificationManager.IMPORTANCE_DEFAULT); notificationChannel.setDescription("This is an example channel for notifications"); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(notificationChannel); }
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Example Title") .setContentText("This is an example notification") .setPriority(NotificationCompat.PRIORITY_DEFAULT);
现在您的应用程序已经设置了通知渠道,您应该能够正确收到和显示通知。
上一篇:AndroidStudio通知