[第一行代码]8.2.1 通知的基本用法

      按书中代码运行无法显示通知;

      原因是Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将通知渠道称之为通知类别。targeSdk升级到26之后,所有的通知的实现都需要提供通知渠道,如果不提供通知渠道的话,所有通知在8.0系统上面都不能正常展示。

      因此NotificationCompat.Builder(this)被替换为NotificationCompat.Builder(this,channelId),并需要创建渠道。

代码如下:

NotificationChannel channel = new NotificationChannel("1",
        "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true); //是否在桌面icon右上角展示小红点
channel.setLightColor(Color.GREEN); //小红点颜色
channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
int notificationId = 0x1234;
Notification notification=new NotificationCompat.Builder(this,"1")
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentTitle("This is contenttitle")
        .setContentText("This is content text")
        .setWhen(System.currentTimeMillis())
        .setPriority(Notification.PRIORITY_MAX)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)).build();
manager.notify(1,notification);
break;

参考:https://blog.csdn.net/rentee/article/details/78303532;

          https://blog.csdn.net/cch___/article/details/78552395

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值