android - Android - 状态栏通知未显示
问题描述
当我的应用程序根据业务完成流程时,我试图在状态栏中显示通知。我尝试创建通知。它执行但在状态栏中看不到。也无法获取错误详细信息。以下是尝试。但两者都没有奏效。
private fun showNotification(title: String?, body: String?) {
val intent = Intent(requireContext(), MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(requireContext(), 0, intent,
PendingIntent.FLAG_ONE_SHOT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(requireContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
}
//------------------------------------------------ -------------------------------------------------- ----
private fun sendNotification(remoteMessage: String) {
val intent = Intent(requireContext(), MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(requireContext(), 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(requireContext())
.setContentText(remoteMessage)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = requireContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}
解决方案
在 Android 8(API 级别 26)中,所有通知都必须分配给一个通道。这对我有用:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext(), CHANNEL_ID)
.setSmallIcon(R.drawable.emo_no_paint_120)
.setContentTitle("title")
.setContentText("content")
.setColor(Color.parseColor("#009add"))
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(
NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(0, mBuilder.build());
你应该添加 AppCompat 库
implementation 'com.android.support:support-compat:27.1.0'
检查这个链接
https://developer.android.com/training/notify-user/channels.html
推荐阅读
- javascript - 如何将构造函数添加到 React 中的功能组件?
- python - Telegram bot Python,如何在触发另一个事件时向某些用户发送消息
- java - 指纹上的Android生物识别UserNotAuthenticatedException
- leaflet - Leaflet js地图弹出过滤
- sas - 为一系列日期创建新列,然后根据每一行的开始日期和结束日期之间的那一天填充 1 或 0
- python - 如何反转数据框列中的所有值?
- android - google play oauth 2.0 flow android studio推荐哪个库?
- python - 将元组列表转换为python中的列表
- embedded - 禁用stm32f103的外部中断后如何重新启用它?
- asp.net - 我应该将信息存储在 Web Api 2 控制器的什么位置?