如果你在使用Android 9的startForeground方法时遇到了通知有问题的情况,可以尝试以下解决方法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My Notification")
.setContentText("This is a notification from my app")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My Notification")
.setContentText("This is a notification from my app")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
通过检查和调整以上代码,你可以解决Android 9的startForeground通知问题。