Android 通知 NotificationManagerCompat

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)

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容