Android 通知 NotificationManagerC

2022-07-13  本文已影响0人  余炳高笔记

Android  通知

/**

* notifyId 相同会覆盖该通知

*/

private fun showNotification(notifyId: Int, context: Context, title: String, subtitle: String) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

// 创建渠道

        NotificationManagerCompat.from(context).createNotificationChannel(

NotificationChannel("NotifyId", "NotifyId", NotificationManager.IMPORTANCE_HIGH)

)

}

// 目标界面

    val hangIntent = Intent(this, LoginActivity::class.java)

hangIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

    val activity = PendingIntent.getActivity(context, 0, hangIntent, 0)

val notification = NotificationCompat

//这里的渠道名就是你自己想展示通知对应的渠道分组

        .Builder(context, "A123")

//点击打开的界面

        .setContentIntent(activity)

//设置状态栏展示的通知样式

        .setSmallIcon(R.mipmap.msgs_curricula1)

//设置通知标题

        .setContentTitle(title)

//设置通知正文

        .setContentText(subtitle)

//渠道编号

        .setChannelId("NotifyId")

//点击通知 消失

        .setAutoCancel(true)

.build()

NotificationManagerCompat.from(context).notify(notifyId, notification)

}

上一篇 下一篇

猜你喜欢

热点阅读