android8.0以上Notification,通知栏通知

​​​​​​参考链接:Notification通知栏 - 掘金

​​​​​​参考链接:

在此之前,需保证手机通知权限已打开

//发起通知栏推送
//此处判断安卓版本号是否大于或者等于Android8.0
public void launchPush() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "chat";//设置通道的唯一ID
        String channelName = "聊天消息";//设置通道名
        int importance = NotificationManager.IMPORTANCE_MAX;//设置通道优先级
        createNotificationChannel(channelId, channelName, importance, title, text);
    } else {
        sendSubscribeMsg(title, text);
    }
}

@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance, String title, String text) {
    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    LogUtils.i("channel---" + channel.getId());//chat
    channel.canBypassDnd();//是否绕过请勿打扰模式
    channel.enableLights(true);//闪光灯
    channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_SECRET);//锁屏显示通知
    channel.setLightColor(Color.RED);//闪关灯的灯光颜色
    channel.canShowBadge();//桌面launcher的消息角标
    channel.enableVibration(true);//是否允许震动
    channel.getAudioAttributes();//获取系统通知响铃声音的配置
    channel.getGroup();//获取通知取到组
    channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
    channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
    channel.shouldShowLights();//是否会有灯光
    NotificationManager notificationManager = (NotificationManager) App.getApplication().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
    sendSubscribeMsg(title, text);
}

public void sendSubscribeMsg(String title, String text) {
    NotificationManager manager = (NotificationManager) App.getApplication().getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(App.getApplication(), "chat")
            .setContentTitle(title)
            .setContentText(text)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_ALL)
            .setLargeIcon(BitmapFactory.decodeResource(App.getApplication().getResources(), R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .build();
    manager.notify(2, notification);
}

通过该方法推送的消息,手动上滑悬浮框后,会进入一段休眠时间,悬浮窗不再出现,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值