【发布时间】:2021-11-06 09:19:04
【问题描述】:
通知工作正常,出现(sdk 28,android 9)。我决定在 android 11 的模拟器上试一试,结果没有出现通知。我无法理解我的错误。适用于任何地方,在 sdk 28 下的小米 Redmi 7 和 Pixel 4 上进行了测试。在 sdk 30 下的 Nexus 5 上,该应用程序可以正常工作,但只是没有通知。
private void createNotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel1 = new
NotificationChannel(CHANNEL_ID_1, "Channel(1)", NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("Channel 1 Dec..");
NotificationChannel channel2 = new
NotificationChannel(CHANNEL_ID_2, "Channel(2)", NotificationManager.IMPORTANCE_HIGH);
channel2.setDescription("Channel 2 Dec..");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel1);
notificationManager.createNotificationChannel(channel2);
}
}
void showNotification(int playPauseBtn){
...
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
.setSmallIcon(playPauseBtn)
.setLargeIcon(thumb)
.setContentTitle(musicFiles.get(position).getTitle())
.setContentText(musicFiles.get(position).getArtist())
.addAction(R.drawable.ic_skip_previous, "Previous", prevPending)
.addAction(playPauseBtn, "Pause", pausePending)
.addAction(R.drawable.ic_skip_next, "Next", nextPending)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(contentPending)
.setDeleteIntent(deletePending)
.setOnlyAlertOnce(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
startForeground(2, notification);
}
【问题讨论】:
-
您应该发布我们可以复制和粘贴的代码进行试用。这里有很多是未定义的。
标签: java android kotlin notifications