常驻通知栏

常驻通知栏设置一个默认通知栏不废话,上代码//创建一个通知管理器 NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 获取Notification实例 Notification ...
摘要由CSDN通过智能技术生成

常驻通知栏

不废话,上代码

//创建一个通知管理器
  NotificationManager  notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  //        获取Notification实例
        Notification notification=new NotificationCompat.Builder(this,channelId)
                .setContentTitle("title")
                .setContentText("content")
                .setWhen(System.currentTimeMillis())
  //             .setAutoCancel(false) 点击通知栏后是否消失
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon))
               // .setCustomContentView(remoteView) // 设置自定义的RemoteView,需要API最低为24
                .setSmallIcon( R.mipmap.instant)
                // 设置点击通知栏后跳转地址
                .setContentIntent( PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0))
                .build();
//        添加渠道
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(channelId, "subscribeName", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("description");
            notificationManager.createNotificationChannel(channel);
        }
                // 设置常驻 Flag
                   notification.flags = Notification.FLAG_ONGOING_EVENT;
                   //展示通知栏
        notificationManager.notify(notificationId,notification);   

展示效果如下
在这里插入图片描述
注意
在8.0以下手机是不用添加渠道的,但在之上不添加渠道不展示通知栏,9.0手机不设置setDescription()依旧不展示通知栏【华为mate10 9.0版本测试】;

自定义通知栏

有时间我们不想展示系统给的固定的展示效果,想自己写一个效果,只需要在代码中添加RemoteViews。

//通过xml创建RemoteViews,并且动态改变布局中的内容
        RemoteViews remoteView = new Remo
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值