Android:自定义通知栏,及清除指定通知栏消息

发送到通知栏

NotificationManager.notify(int id, Notification notification) ;

删除对应的通知消息

NotificationManager.cancel(id);




下面的案例调用方式在最下面

在这里插入图片描述



在Application自定义一个通知栏

安卓7.0后好像不太支持从后台直接唤起程序,我就想到了从通知栏获取进入,事实证明还是可以达到目的的

private static final int VISIBILITY_SCREEN = -1000;
private NotificationManager mManager;

主要方法

/**
     * 发送自定义通知栏
     */
    private void sendCustomNotify(String nickname) {
        Notification.Builder builder = creatNotification();

        RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notify_layout);
        remoteView.setTextViewText(R.id.notify_title, nickname);
        remoteView.setTextViewText(R.id.notify_content, "来自数字孪生的一则消息");
        //即将发生的意图可以取消可以更新

        /*
        * 点击进入会话
        * */

        PendingIntent pendingIntent = PendingIntent.getActivity(this, -1,
                new Intent(this, FragmentManagerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

        /*
        * 直接挂断
        * */
        remoteView.setOnClickPendingIntent(R.id.ring_hangoff, pendingIntent);
        /*
         * 视频聊天
         * */
        remoteView.setOnClickPendingIntent(R.id.ring_pickup, pendingIntent);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.setCustomContentView(remoteView);
        }
        mManager.notify(3, builder.build());
    }

    /**
     * 封装一个通知builder
     *
     * @return
     */
    private Notification.Builder creatNotification() {
        Notification.Builder builder = new Notification.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("消息来了")
                .setContentText("消息通知内容")
                .setSmallIcon(R.drawable.logo);
        //适配8.0以上通知栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //第三个参数设置通知的优先级别
            NotificationChannel channel =
                    new NotificationChannel("channel_id", "app_msg", NotificationManager.IMPORTANCE_DEFAULT);
            channel.canBypassDnd();//是否可以绕过请勿打扰模式
            channel.canShowBadge();//是否可以显示icon角标
            channel.enableLights(true);//是否显示通知闪灯
            channel.enableVibration(true);//收到小时时震动提示
            channel.setBypassDnd(true);//设置绕过免打扰
            channel.setLockscreenVisibility(VISIBILITY_SCREEN);
            channel.setLightColor(Color.RED);//设置闪光灯颜色
            channel.getAudioAttributes();//获取设置铃声设置
            channel.setVibrationPattern(new long[]{100, 200, 100});//设置震动模式
            channel.shouldShowLights();//是否会闪光
            mManager.createNotificationChannel(channel);
            builder.setChannelId("channel_id");//这个id参数要与上面channel构建的第一个参数对应
        }
        return builder;
    }

图片自己充填一下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/date_background"
    android:padding="10dp"
    android:orientation="horizontal">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <ImageView
                android:padding="8dp"
                android:src="@drawable/ic_callicon"
                android:layout_width="40dp"
                android:layout_height="match_parent"/>


            <TextView
                android:textColor="#ffffff"
                android:layout_gravity="center_vertical"
                android:id="@+id/notify_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="18dp"
                android:gravity="center_vertical"
                android:text="标题" />
        </LinearLayout>



        <TextView
            android:textColor="#ffffff"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:id="@+id/notify_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center_vertical"
            android:text="来自数字孪生的一则消息" />


    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="60dp"
        android:layout_height="match_parent">
        <ImageView
            android:padding="10dp"
            android:id="@+id/ring_hangoff"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:src="@drawable/icon_hangup"/>
        <TextView
            android:textColor="#ffffff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12dp"
            android:gravity="center"
            android:text="挂断" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="60dp"
        android:layout_height="match_parent">
        <ImageView
            android:padding="10dp"
            android:id="@+id/ring_pickup"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:src="@drawable/icon_pickup"/>
        <TextView
            android:textColor="#ffffff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:gravity="center"
            android:text="接听" />
    </LinearLayout>
</LinearLayout>

启用

sendCustomNotify(send_nick);

清除

  mManager.cancel(3);
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wenlong Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值