1:問題:
使用三方推送,在Android 10顯示通知欄,有時顯示有時不顯示,略顯尷尬
2:解決:
private fun processCustomMessage(context: Context, message: String) {
val channelID = "1001"
val channelName = "bs_notify"
//點擊通知跳轉界面
val intent = Intent(context, BNoTitleWebActivity::class.java)
val bundle = Bundle()
bundle.putString(AppConstant.NEW_URL, "TZGG_URL")
intent.putExtras(bundle)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val pendingIntent = PendingIntent.getActivity(context, 1000, intent, PendingIntent.FLAG_UPDATE_CURRENT)
//通知欄顯示
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val builder: Notification.Builder
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = Notification.Builder(context, channelID)
val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
} else {
builder = Notification.Builder(context)
}
builder.setAutoCancel(true)
.setContentText(gson.fromJson(message, OnNoticePushBean::class.java).PushContent)
.setContentTitle(gson.fromJson(message, OnNoticePushBean::class.java).PushTitle)
.setSmallIcon(R.mipmap.icon_app)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
notificationManager.notify((System.currentTimeMillis() / 1000).toInt(), builder.build())
}
親測可用
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/261762.html
標籤:其他
下一篇:ItemDecoration用法
