Android实现横幅通知

代码如下:

   

 /**
     * 通知栏(兼容android 8.0以上)
     */
    boolean isVibrate=true;//是否震动
    //1.获取消息服务
    NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //默认通道是default
    String channelId="default";
    //2.如果是android8.0以上的系统,则新建一个消息通道
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channelId="chat";
        /*
         通道优先级别:
         * IMPORTANCE_NONE 关闭通知
         * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
         * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
         * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
         * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
         */
        NotificationChannel channel=new NotificationChannel(channelId,"消息提醒",NotificationManager.IMPORTANCE_HIGH);
        //设置该通道的描述(可以不写)
        //channel.setDescription("重要消息,请不要关闭这个通知。");
        //是否绕过勿打扰模式
        channel.setBypassDnd(true);
        //是否允许呼吸灯闪烁
        channel.enableLights(true);
        //闪关灯的灯光颜色
        channel.setLightColor(Color.RED);
        //桌面launcher的消息角标
        channel.canShowBadge();
        //设置是否应在锁定屏幕上显示此频道的通知
        //channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        if (isVibrate) {
            //是否允许震动
            channel.enableVibration(true);
            //先震动1秒,然后停止0.5秒,再震动2秒则可设置数组为:new long[]{1000, 500, 2000}
            channel.setVibrationPattern(new long[]{1000,500,2000});
        } else {
            channel.enableVibration(false);
            channel.setVibrationPattern(new long[]{0});
        }
        //创建消息通道
        manager.createNotificationChannel(channel);
    }
    //3.实例化通知
    NotificationCompat.Builder nc = new NotificationCompat.Builder(this, channelId);
    //通知默认的声音 震动 呼吸灯
    nc.setDefaults(NotificationCompat.DEFAULT_ALL);
    //通知标题
    nc.setContentTitle("标题");
    //通知内容
    nc.setContentText("内容");
    //设置通知的小图标
    nc.setSmallIcon(android.R.drawable.ic_popup_reminder);
    //设置通知的大图标
    nc.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    //设定通知显示的时间
    nc.setWhen(System.currentTimeMillis());
    //设置通知的优先级
    nc.setPriority(NotificationCompat.PRIORITY_MAX);
    //设置点击通知之后通知是否消失
    nc.setAutoCancel(true);
    //点击通知打开软件
    Context application = getApplicationContext();
    Intent resultIntent = new Intent(application, MainActivity.class);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(application, 0, resultIntent, 0);
    nc.setContentIntent(pendingIntent);
    //4.创建通知,得到build
    Notification notification = nc.build();
    //5.发送通知
    manager.notify(1, notification);

在这里插入图片描述

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值