android - 为什么在 Android 9 上不显示通知
问题描述
在 Android 9.0 上不显示通知,在 8 及更低版本上运行良好。我在使用旧代码时遇到了问题,我读到需要在新的 android 版本上通过 Channel,我从互联网上获得了此代码,但仍然无法在 Android 9.0 上运行。我不知道为什么。任何帮助将不胜感激。
当我通过互联网阅读时,一切看起来都很好。但通知不显示。
private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
String CHANNEL_ID = Constants.ChannelID;
String CHANNEL_NAME = "Notification";
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);;
NotificationCompat.Builder notification;
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.notification),
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build());
channel.canShowBadge();
notificationManager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
} else {
notification = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
}
notification
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
.setContentText(message)
.build();
notificationManager.notify(CHANNEL_ID, 1, notification.build());
}
解决方案
我觉得问题是你没setChannelId()
上notificationBuilder
。(我冒昧地重命名notification
为,notificationBuilder
因为它就是这样。)
我在其中一行代码中删除了多余的分号。
另请注意,这canShowBadge()
是一个返回值但不设置任何通知设置的类方法。我用setShowBadge(true)
.
请阅读我在代码片段中留下的评论以获取更多信息。
private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
String CHANNEL_ID = Constants.ChannelID;
String CHANNEL_NAME = "Notification";
// I removed one of the semi-colons in the next line of code
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.notification),
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build());
//channel.canShowBadge();
// Did you mean to set the property to enable Show Badge?
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
.setContentText(message);
// Removed .build() since you use it below...no need to build it twice
// Don't forget to set the ChannelID!!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationBuilder.setChannelId(ID_SPECIAL_OFFER_NOTIFICATION);
}
notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
}
推荐阅读
- html - Vue:改变模板元素的解析方式,保持自闭标签
- reactjs - `getStaticPaths() 获取所有帖子时是否会影响性能,哪些帖子总共是一百万个帖子?
- django - Django HttpResponseRedirect 与重定向
- python - 将数字 pi 转换为字符串“pi”
- flask - socket.io 客户端无法连接到服务器
- reactjs - 在反应应用程序上获取请求失败 404 错误
- azure - 如何将 Azure Functions v3 迁移到 .net core 5.0
- android - 模糊库 - 如何模糊单个视图?
- excel - 如何使用 VBA 变量在 VLookup 中跨多个工作簿的查找值
- r - 如何使用 ggplot barplot 绘制表函数的结果(x 轴值问题)