Notification 使用入门

Notification 的创建

notification 的使用主要有两个关键点:

通过 NotificationCompat.Builder 来设置 Notification;
// 1 context 用来 构建 remote views
// 2 channel_id 用来给 notification 分组
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"chaneel_id");

builder.setContentTitle("notification title")// notification 标题
        .setContentText("notification short message")// notification 段内容
        .setSmallIcon(R.mipmap.ic_launcher)// notification 小图标
通过 NotificationManager 来更新 Notification;
// 创建 notification manager
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification 的使用

notification 的简单使用
NotificationCompat.Builder channelId_1 = new NotificationCompat.Builder(this, "channelId_1");

channelId_1.setContentTitle("简单 Notification")
           .setSmallIcon(R.mipmap.test1)
           .setContentText("普通的 notification, 可滑动删除!.")
           .setAutoCancel(true);

mManager.notify(id, channelId_1.build());
显示多行文字
  String content = "到南京时,有朋友约去游逛,勾留7了一日;第二日上午便须渡江到浦口,下午上车北去。父亲因为事忙,本已说定不送我,叫旅馆里一个熟识的茶房8陪我同去。他再三嘱咐茶房,甚是仔细。但他终于不放心,怕茶房不妥帖9;颇踌躇10了一会。其实我那年已二十岁,北京已来往过两三次,是没有什么要紧的了。他踌躇了一会,终于决定还是自己送我去。我再三劝他不必去;他只说:“不要紧,他们去不好!”\n" +
                "我们过了江,进了车站。我买票,他忙着照看行李。行李太多,得向脚夫11行些小费才可过去。他便又忙着和他们讲价钱。我那时真是聪明过分,总觉他说话不大漂亮,非自己插嘴不可,但他终于讲定了价钱;就送我上车。他给我拣定了靠车门的一张椅子;我将他给我做的紫毛大衣铺好座位。他嘱我路上小心,夜里要警醒些,不要受凉。又嘱托茶房好好照应我。我心里暗笑他的迂;他们只认得钱,托他们只是白托!而且我这样大年纪的人,难道还不能料理自己么?我现在想想,我那时真是太聪明了。\n" +
                "我说道:“爸爸,你走吧。”他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。我看见他戴着黑布小帽,穿着黑布大马褂12,深青布棉袍,蹒跚13地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n" +
                "近几年来,父亲和我都是东奔西走,家中光景是一日不如一日。他少年出外谋生,独力支持,做了许多大事。哪知老境却如此颓唐!他触目伤怀,自然情不能自已。情郁于中,自然要发之于外;家庭琐屑便往往触他之怒。他待我渐渐不同往日。但最近两年不见,他终于忘却我的不好,只是惦记着我,惦记着他的儿子。我北来后,他写了一信给我,信中说道:“我身体平安,惟膀子疼痛厉害,举箸14提笔,诸多不便,大约大去之期15不远矣。”我读到此处,在晶莹的泪光中,又看见那肥胖的、青布棉袍黑布马褂的背影。唉!我不知何时再能与他相见";


NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setContentTitle("背影-朱自清")
       .setSmallIcon(R.mipmap.ic_launcher_round)
       .setContentText(content);

// 通过 big text style 来设置 显示多行文字
new NotificationCompat.BigTextStyle(builder)
                      .setBigContentTitle("背影")
                      .bigText(content)
                      .setSummaryText("朱自清文稿");


mManager.notify(id, builder.build())

这里写图片描述

显示大图
  NotificationCompat.Builder channelId_3 = new NotificationCompat.Builder(this, channelId);
        channelId_3.setContentTitle("猛虎图")
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentText("点击显示大图");

        new NotificationCompat.BigPictureStyle(channelId_3)
                .bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.test1))
                .setBigContentTitle("东北猛虎")
                .setSummaryText("来自东北的猛虎~~");

        mManager.notify(id, channelId_3.build());

这里写图片描述

显示多行列表
 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_4");
builder.setContentTitle("新世界新闻")
       .setContentText("24 小时新闻速报")
       .setSmallIcon(R.mipmap.ic_launcher_round);

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

inboxStyle.setBigContentTitle("中国新闻");

for (int i = 0; i < 10; i++) {
    inboxStyle.addLine("大连新闻 " + i);
   }

builder.setStyle(inboxStyle);
mManager.notify(4, builder.build());

这里写图片描述

显示常驻 notification ,不可滑动删除,不可 全部清理删除
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_5");
        builder.setContentTitle("常驻通知")
                .setContentText("这个通知不能通过滑动删除!")
                .setSmallIcon(R.mipmap.ic_launcher_round);

        Notification notification = builder.build();
        // 这个 notification 提示正在运行的事情
        notification.flags = NotificationCompat.FLAG_ONGOING_EVENT;
        // 设置这个 flag 不能通过全部清除 删除 notification
        notification.flags = NotificationCompat.FLAG_NO_CLEAR;

        mManager.notify(5, notification);
点击 notification 跳转到 SecActivity

这里用到了PendingIntent 官网介绍.

Intent intent = new Intent(this, SecActivity.class);
intent.putExtra(SecActivity.KEY_MESSAGE, "这是传送过来的 消息~");

PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_5");
 builder.setContentTitle("跳转 SecActivity")
         .setContentText("传递消息")
         .setContentIntent(pendingIntent)
         .setSmallIcon(R.mipmap.ic_launcher_round);

mManager.notify(6, builder.build());
显示 横幅 通知
Intent intent = new Intent(this, SecActivity.class);
intent.putExtra(SecActivity.KEY_MESSAGE, "从横幅通知穿过来的消息");

PendingIntent pendingIntent=PendingIntent.getActivity(this,102,intent,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_6");
builder.setContentTitle("横幅通知")
       .setContentText("这是个很重要的消息所以要横幅显示哦~")
       .setDefaults(NotificationCompat.DEFAULT_ALL)
       .setFullScreenIntent(pendingIntent, true)// 通过这个设置 显示横幅通知
       .setSmallIcon(R.mipmap.ic_launcher_round);

mManager.notify(7, builder.build());

这里写图片描述

自定义 View 的 notification

这里用到了 RemoteViews ,个人感觉写的比较好的一篇 王月半子 的博客Android神奇“控件”—–RemoteViews 写的比较好,这是 RemoteView 的官方文档

RemoteViews views = new RemoteViews(getPackageName(), R.layout.remote_view);

views.setTextViewText(R.id.remote_title, "两只老虎");// 通过 id 设置 text
views.setTextViewText(R.id.remote_user, "--- 民谣");

List<String> song = newLyrics();


Intent start = new Intent();
start.setAction("START_SONG");
views.setOnClickFillInIntent(R.id.remote_start, start);// 通过 id 设置 点击事件

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_7");

builder.setContentTitle("自定义 View")
          .setContentText("自定义的 remote views")
          .setCustomBigContentView(views)
          .setSmallIcon(R.mipmap.ic_launcher_round);

mManager.notify(8, builder.build());

这里写图片描述

显示待进度条的 notification

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_9");

builder.setContentTitle("下载")
       .setContentText("下载 APK")
       .setSmallIcon(R.mipmap.ic_launcher_round);

 new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(1000);
                        // 最后一个参数 indeterminate 如果设置为 true 表示当前 process 不确定,
                        // notification 中的 process bar 会来回滑动
                        builder.setProgress(10, i, false);
                        mManager.notify(9, builder.build());

                        if (i == 9) {
                            builder.setProgress(0, 0, false);
                            builder.setContentText("下载完成!");
                            mManager.notify(9, builder.build());
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

以上就是 notification 的简单用法,下面介绍一下 相关的 API

NotificationCompat.Builder

这里使用了r17171709 的一部分总结,再次表示感谢.

1 setWhen(long when)// 不使用 系统时间想要自定义时间 可以通过这个设置
// true 点击后删除,
// false 点击不删除, 滑动可删除
2 setAutoCancel(boolean autoCancel)

// 默认情况 notification 显示排序是按照时间排列的,最新的 notification 在最上面
// 如果设置 priority 则会按照 优先级排序
// 优先级 最大为 2, 最小为 -2
3 setPriority(int pri)

// 设置 notification提醒方式
//NotificationCompat#DEFAULT_SOUND 设置声音,使用默认铃声
//NotificationCompat#DEFAULT_VIBRATE 设置震动
//NotificationCompat#DEFAULT_LIGHT 设置手机呼吸灯
//NotificationCompat.DEFAULT_ALL 全部
4 setDefaults(int defaults)

// 如果想使用自定义铃声,可通过以下方式设置
5 notification.sound = Uri.parse("file:///sdcard0/notification.ogg");

// 默认振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
// 自定义振动
//震动效果,表示在100、200、300、400这些时间点交替启动和关闭震动
long[] vibrate = {100, 200, 300, 400};
notification.vibrate = vibrate;

// 默认闪烁
notification.defaults |= Notification.DEFAULT_LIGHTS;
// 自定义闪烁
notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯
notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒
notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒
notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值