android 8.0 原有的通知直接失效,因为android 8.0 以后通知增加分组,如果不进行分组通知将不能正常发送。

class MainOreoActivity : AppCompatActivity() {    var TAG = "tag"    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        TAG += localClassName        setContentView(R.layout.activity_oreo_main)        oreoText.text = getString(R.string.android_oreo)        testNotify.text = getString(R.string.oreo_notify_test)        testNotify.setOnClickListener({ testNotify() })    }    fun testNotify() {        Log.d(TAG, getString(R.string.oreo_notify_test))        val id = 1000        val channelId = "channel_first"        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)            createChannelId(channelId)        val build: NotificationCompat.Builder = NotificationCompat.Builder(this, channelId)             build.setContentTitle("test")//设置标题                .setContentText(getString(R.string.oreo_notify_test))//设置内容                .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher_round))//设置通知栏图标                .setSmallIcon(R.mipmap.ic_launcher)//设置状态栏图标                .setAutoCancel(true)//点击自动关闭                /** setPriority() 方法共有5个等级:                1. PRIORITY_MIN - 最低级别(-2);                2. PRIORITY_LOW - 较低级别(-1);                3. PRIORITY_DEFAULT - 默认级别(0);                4. PRIORITY_HIGH - 较高级别(1);                5. PRIORITY_MAX - 最高级别(2);                 */            .priority = NotificationCompat.PRIORITY_HIGH        val n: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager        n.notify(id, build.build())    }    /**     * 创建分类     */    @RequiresApi(Build.VERSION_CODES.O)    private fun createChannelId(channel: String) {        val ncGroup = NotificationChannelGroup("group_second", "通知渠道")        val n: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager        n.createNotificationChannelGroup(ncGroup)        val chan = NotificationChannel(channel, "通知渠道", NotificationManager.IMPORTANCE_DEFAULT)        //锁屏的时候是否展示通知        // VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容;        // VISIBILITY_PUBLIC : 显示通知的全部内容;        // VISIBILITY_SECRET : 不显示任何内容,包括图标。        chan.lockscreenVisibility = Notification.VISIBILITY_PUBLIC        n.createNotificationChannel(chan)    }}


更多相关文章

  1. Android获取Apk文件图标信息
  2. android拨打电话流程分析
  3. Android(安卓)RecyclerView更新删除单条数据
  4. [Android]Toolbar使用详解(三)——源码解析
  5. Android沉浸式标题栏状态栏字体颜色修改(小米和魅族以及Android6.
  6. flutter集成推送功能-小米推送集成
  7. android p状态栏,插入sim卡,关闭数据流量,状态栏卡图标右下角有X图标
  8. android解析json小例子
  9. 通过设置android:imeOptions来改变软键盘Enter键图标

随机推荐

  1. Unity打包——Android和IOS
  2. mac电脑开发android,找不到国产设备
  3. 修改Android系统属性SystemProperties.se
  4. Android(安卓)ImageView用法(xml)
  5. Android使用AIDL跨进程数据共享
  6. Android各种类型Dialog点击空白处自动关
  7. Android模拟器使用SD卡
  8. 搭建一个android 简单的框架应用
  9. Android : 一键分享(纲领)
  10. Android 2.2开始支持安装APP2SD卡上