Android安卓状态栏消息推送通知(Notification)

我从不猜测,猜测是一个很坏的习惯——会影响正常的逻辑推理能力。              ——阿瑟·柯南·道尔 《福尔摩斯探案集》


近日,在做安卓项目开发的时候涉及到状态栏通知的需求,查了资料,总结一个简单有效的方法,下面放上代码(Kotlin版):

//该代码为Kotlin,读者可根据需求稍作调整
    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    var notification: Notification
    //点击通知栏的消息时跳转的页面
    val intent = PendingIntent.getActivity(this,
            100, Intent(this, MainActivity::class.java),
            PendingIntent.FLAG_CANCEL_CURRENT)
    //消息通知显示的大图标
    val largeIcon = BitmapFactory.decodeResource(
            this.resources, android.R.drawable.ic_media_play)
    //该出做了一个判断,因为在安卓O系统中需要新建一个容器来存放通知
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        //定义通知的重要程度,即影响程度
        val mChannel = NotificationChannel("id", "name",NotificationManager.IMPORTANCE_LOW)
        notificationManager.createNotificationChannel(mChannel)
        notification = Notification.Builder(this)
                .setChannelId("id")
                .setContentTitle("标题")
                .setContentText("内容内容内容内容内容内容内容内容内容内容")
                .setContentIntent(intent)
                .setLargeIcon(largeIcon)
                //设置消息通知显示的小图标
                .setSmallIcon(android.R.drawable.ic_media_play).build()
    }else{
        val builder = NotificationCompat.Builder(this)
                .setContentTitle("标题")
                .setContentText("内容内容内容内容内容内容内容内容内容内容")
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setAutoCancel(true)
                .setLargeIcon(largeIcon)
                .setSmallIcon(android.R.drawable.ic_media_play)
                .setContentIntent(intent)
        notification = builder.build()
    }
    notificationManager.notify(0,notification)

补充一点, Notification通知会有通知影响的权限高低,这时候我们需要改变权限的高低,比如通知的时候是否发出声音,是否有弹出框等等。

//NotificationManager.IMPORTANCE_HIGH为通知权限高,同理根据需求来调整通知权限
val mChannel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH)

我们在开发中其实会有不同的通知,那么我们就不能在每一处通知都加上上述代码,则需要新建一个公共类来保存通知信息,只需要传入相关参数即可:

class MyNotification {

    companion object {
        fun putNotification(nManager: NotificationManager, act: Activity, mtitle: String, mtext: String) {
            //val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            var notification: Notification
            val intent = PendingIntent.getActivity(act,
                    100, Intent(act, MainActivity::class.java),
                    PendingIntent.FLAG_CANCEL_CURRENT)
            val largeIcon = BitmapFactory.decodeResource(
                    act.resources, android.R.drawable.ic_media_play)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val mChannel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH)
                nManager.createNotificationChannel(mChannel)
                notification = Notification.Builder(act)
                        .setChannelId("id")
                        .setContentTitle(mtitle)
                        .setContentText(mtext)
                        .setContentIntent(intent)
                        .setAutoCancel(true)
                        .setLargeIcon(largeIcon)
                        .setSmallIcon(android.R.drawable.ic_media_play).build()
            } else {
                val builder = NotificationCompat.Builder(act)
                        .setContentTitle(mtitle)
                        .setContentText(mtext)
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setAutoCancel(true)
                        .setLargeIcon(largeIcon)
                        .setSmallIcon(android.R.drawable.ic_media_play)
                        .setContentIntent(intent)
                notification = builder.build()
            }
            nManager.notify(0, notification)
        }
    }
}

注意 “companion object” ,之前我遇到了一个问题,调用该类的方法时,一直提示错误该类未初始化。经过很多次的尝试,那么解决办法就是在该类加上 companion object { } 。

你可能感兴趣的:(原创,移动开发:Android,Android开发讨论组)