Android 9通知栏(基础)

Android 9通知栏(基础)

发送通知

首先是一个最简单的通知
		//1,获取通知管理器
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		//2,通知渠道
        NotificationChannel channel = new NotificationChannel("wendu", "温度", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);//注册到通知管理器
        Notification.Builder builder = new Notification.Builder(this, "wendu");//这里要和注册的渠道id要一致
        builder.setSmallIcon(R.drawable.image)//这是必须的
                .setChannelId("wendu")//在这里设置会覆盖上面的设置
                .setContentTitle("测试")
                .setContentText("这是测试内容");
        notificationManager.notify(1, builder.build());
自定义视图
   //自定义布局的通知
    public void SelfNofition(View view) {
        NotificationChannel notifici = new NotificationChannel("self", "自定义", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(notifici);
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.notifici);//自定义的布局视图
        views.setTextViewText(R.id.fazhi, "警告:当前阀值:200/120");

        Notification.Builder builder = new Notification.Builder(this, "self");
        builder.setSmallIcon(R.drawable.qq)//使用RemoteViews时,设置的是状态栏中的小图标,必须要设置
                .setContent(views);

        notificationManager.notify((int) System.currentTimeMillis(), builder.build());
    }

对自定义的布局中,最外层的布局似乎要用mach才能让通知栏滑动取消。

设置渠道分组
public class MainActivity extends AppCompatActivity {

    //通知管理器
    private NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1,获取通知管理器
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //设置群组(不是必须的)
        notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("group", "groupname"));
    }

    public void Send(View view) {
        String id = "test";
        String name = "ad";

        //2,通知

        //渠道id
        Notification.Builder builder = new Notification.Builder(this, "ad");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        	//Android 版本本人为28,即Android9。 后面的是Android的版本
            //3,通知渠道
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
            Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();

            Log.i("info123", mChannel.toString());

            // NotificationChannel{mId='my_channel_01', mName=我是渠道名字,
            // mDescription=, mImportance=2, mBypassDnd=false, mLockscreenVisibility=-1000,
            // mSound=content://settings/system/notification_sound,
            // mLights=false, mLightColor=0, mVibration=null, mU
            mChannel.setGroup("group");
            //将通知渠道创建到通知管理器
            notificationManager.createNotificationChannel(mChannel);
//
            RemoteViews views = new RemoteViews(getPackageName(), R.layout.notifici);//自定义的布局视图
            views.setTextColor(R.id.mytext, Color.GREEN);
            views.setTextViewText(R.id.mytext, "这是自定义的文字");
            views.setImageViewResource(R.id.iamge, R.drawable.ic_launcher_background);

            //设置通知属性
            builder = new Notification.Builder(this, "ad");
            builder.setSmallIcon(R.drawable.image)//使用RemoteViews时,设置的是状态栏中的小图标,必须要设置
                    .setAutoCancel(true)//设置是否点击通知后会自动消失
                    .setChannelId(id)
                    .setContent(views)
                    .setContentTitle("我是广告")
                    .setContentText("内容:恰饭时间到了,Oreo is Coming.")
                    .setNumber(4)//设置通知集合的数量
                    .setWhen(System.currentTimeMillis())//设置展示时间
                    .setTicker("通知来了");

            Intent resultIntent = new Intent(this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            builder.setContentIntent(resultPendingIntent);
        } else {

        }
        //通过通知管理器发送通知
        //标识应用程序中此通知的唯一标识符。根据渠道ID发送通知
        //一个对象对象,描述要向用户显示什么。不能为空。
        notificationManager.notify((int) System.currentTimeMillis(), builder.build());
    }
}

通知栏从Android8.0开始,便需要加入渠道信息才能够正常显示通知了。

参考

参考:详细介绍,Demo

自定义通知布局

详细介绍

自定义布局设置布局文件数据

使在一组中的通知,进行数据的更替。在另一组中就打开新的通知

Android 8.0 后通知、通知渠道、通知渠道组使用简介

在这里插入图片描述

最后,一定要注意手机的通知权限。和通知的重要程度。

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值