栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android(三十一):Notification 状态栏通知

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android(三十一):Notification 状态栏通知

展示

源码


    
    
var btn01 = (Button)FindViewById(Resource.Id.btn_01);
var btn02 = (Button)FindViewById(Resource.Id.btn_02);
var largeBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.avatar);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);

if (btn01 != null)
    btn01.Click += (sender, args) =>
    {
        Notification notification;
        const string id = "ChannelId";
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            // id:          每个包必须是唯一的,太长,可能会被截断
            // name:        用户可见名称,最大建议长度为40个字符,太长可能会被截断
            // importance:  重要性
            var notificationChannel = new NotificationChannel(id, "name", Notificationimportance.High);
            notificationManager?.CreateNotificationChannel(notificationChannel);
            notification = new Notification.Builder(this)
                .SetChannelId(id)
                .SetContentTitle("SetContentTitle")
                .SetContentText("SetContentText")
                .SetSubText("SetSubText")
                .SetTicker("SetTicker")
                .SetWhen(JavaSystem.CurrentTimeMillis()) // 设置通知时间
                .SetSmallIcon(Resource.Drawable.small_icon) // 小图标
                .SetLargeIcon(largeBitmap) // 大图标
                .SetAutoCancel(false) // 用户点击Notification点击面板后是否让通知取消(默认不取消)
                .SetDefaults(NotificationDefaults.All)
                ?.Build();
        }
        else
        {
            notification = new NotificationCompat.Builder(this)
                .SetContentTitle("SetContentTitle")
                .SetContentText("SetContentText")
                .SetSubText("SetSubText")
                .SetTicker("SetTicker")
                .SetWhen(JavaSystem.CurrentTimeMillis()) // 设置通知时间
                .SetSmallIcon(Resource.Drawable.small_icon) // 小图标
                .SetLargeIcon(largeBitmap) // 大图标
                .SetAutoCancel(false) // 用户点击Notification点击面板后是否让通知取消(默认不取消)
                .SetDefaults((int)NotificationDefaults.Sound)
                .Build();
        }

        notificationManager?.Notify(1, notification);
    };

if (btn02 != null)
    btn02.Click += (sender, args) => notificationManager?.Cancel(1);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号