Notification通知权重问题

因为前几天在网上看的关于通知栏的案例都是几年前的Demo了
这里将自己从Android官方学习的心得分享下
小白当然不知道通知为什么有时候可以悬浮,有时候只是存在于状态栏--------下面进行这方面讲解

首先创建Notification实例化对象 ,Android官方在API26后采用构造者模式进行实例化:

        var builder = NotificationCompat.Builder(this,"id")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("我的标题")
                .setContentText("${i++}%")
                .setShortcutId("1")
//                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
//                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setProgress(100,i,false)

在新版后需要我们创建通知渠道
(权重问题的核心代码 下面进行讲解)

   val channel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH).apply {
            /*用户可见的重要性级别	重要性(Android 8.0 及更高版本)	优先级(Android 7.1 及更低版本)
    紧急
        :发出提示音,并以浮动通知的形式显示	IMPORTANCE_HIGH	PRIORITY_HIGH 或 PRIORITY_MAX
    高
        :发出提示音	IMPORTANCE_DEFAULT	PRIORITY_DEFAULT
    中
        :无提示音	IMPORTANCE_LOW	PRIORITY_LOW
    低
        :无提示音,且不会在状态栏中显示。	IMPORTANCE_MIN	PRIORITY_MIN
*/
            description = "description"
        }

获取系统的服务


        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)

发送通知


        notificationManager.notify(1, builder.build())

这里我们将app运行就会弹出一条通知了

当我们的通知渠道实例化时
@Importance int importance
就是我们要设置通知的权重了

 /**
     * Creates a notification channel.
     *
     * @param id The id of the channel. Must be unique per package. The value may be truncated if
     *           it is too long.
     * @param name The user visible name of the channel. You can rename this channel when the system
     *             locale changes by listening for the {@link Intent#ACTION_LOCALE_CHANGED}
     *             broadcast. The recommended maximum length is 40 characters; the value may be
     *             truncated if it is too long.
     * @param importance The importance of the channel. This controls how interruptive notifications
     *                   posted to this channel are.
     */
    public NotificationChannel(String id, CharSequence name, @Importance int importance) {
        this.mId = getTrimmedString(id);
        this.mName = name != null ? getTrimmedString(name.toString()) : null;
        this.mImportance = importance;
    }

权重参数:/*用户可见的重要性级别 重要性(Android 8.0 及更高版本) 优先级(Android 7.1 及更低版本) 紧急 :发出提示音,并以浮动通知的形式显示 IMPORTANCE_HIGH PRIORITY_HIGH 或 PRIORITY_MAX 高 :发出提示音 IMPORTANCE_DEFAULT PRIORITY_DEFAULT 中 :无提示音 IMPORTANCE_LOW PRIORITY_LOW 低 :无提示音,且不会在状态栏中显示。 IMPORTANCE_MIN PRIORITY_MIN */

最后要注意我们进行权重的修改时候要换通知渠道的id
不然不会更新你之前权重的操作

最后联用Service实现的小Demo(后台下载通知进度)

package com.example.myapplication.Notfition_Service

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.example.myapplication.R


class Notfition_Service : Service() {
    var i  = 0;

    override fun onBind(intent: Intent): IBinder {
        return Binder()
    }

    @RequiresApi(Build.VERSION_CODES.O)   //注解API版本
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        var i  = 0 ;
        Thread{
            while (true){
        var builder = NotificationCompat.Builder(this,"id")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("我的标题")
                .setContentText("${i++}%")
                .setShortcutId("1")
//                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
//                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setProgress(100,i,false)
        val channel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW).apply {
            /*用户可见的重要性级别	重要性(Android 8.0 及更高版本)	优先级(Android 7.1 及更低版本)
    紧急
        :发出提示音,并以浮动通知的形式显示	IMPORTANCE_HIGH	PRIORITY_HIGH 或 PRIORITY_MAX
    高
        :发出提示音	IMPORTANCE_DEFAULT	PRIORITY_DEFAULT
    中
        :无提示音	IMPORTANCE_LOW	PRIORITY_LOW
    低
        :无提示音,且不会在状态栏中显示。	IMPORTANCE_MIN	PRIORITY_MIN
*/
            description = "description"
        }
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
        notificationManager.notify(1, builder.build())
                Thread.sleep(2000)
            }
        }.start()
        return super.onStartCommand(intent, flags, startId)
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值