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

Notification消息通知的简单使用

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

Notification消息通知的简单使用

前言

在这里我把Notification的使用大概分为两点

1.使用Notification.Builder(通知构建器)构建一个通知,在其中设置他的相关属性,
android 8 及以上必须设置NotificationChannel(通知渠道),最后不要忘了调用.build()方法构建通知。
2.NotificationManager(通知管理器),管理器主要调用createNotificationChannel()方法创建通过渠道,
以及最后调用notify()方法发出通知。


一、布局

布局文件:添加了三个Button实现三种通知的效果



    
二.普通通知

代码如下(示例):

//在点击了通知后会跳转到百度网页
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
       //pendingIntent可以看作一个对Intent的封装,不会立刻执行
       PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
       NotificationChannel mChannel = new NotificationChannel("001", "普通通知", NotificationManager.IMPORTANCE_LOW);
       Notification notification = new Notification.Builder(this)
                    .setChannelId("001")//通知渠道id
                    .setContentTitle("普通通知")//标题
                    .setContentText("您有一项新活动")//内容
                    .setSmallIcon(R.drawable.ic_launcher_foreground)//小图标
                    .setContentIntent(pendingIntent)//设置内容跳转
                    .build();//构建通知
       //获取通知管理器
       NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
       notificationManager.createNotificationChannel(mChannel);//创建通知渠道
       notificationManager.notify(0, notification);//发出通知
二.折叠式通知

折叠式通知 (是一种自定义视图的Notification)用来显示长文本和
一些自定义的布局场景,在使用中就是多定义了RemoteViews,调用了notification1.bigContentView()方法

//在点击了通知后会跳转到百度网页
   Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
   PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,0);
   Notification notification = new Notification.Builder(this)
                .setContentTitle("折叠式通知")
                .setContentText("您有一项新活动")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setChannelId("002")
                .build();
    //设置展开后的视图
    RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.view_inside);
    notification.bigContentView = remoteViews;
    NotificationChannel channel = new NotificationChannel("002","折叠式通知",NotificationManager.IMPORTANCE_LOW);
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);
    manager.notify(1,notification);

展开后的布局:


    
    

二.悬挂式通知

代码如下(示例):

//在点击了通知后会跳转到百度网页
   Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
   PendingIntent pendingIntent = PendingIntent.getActivity(this,2,intent,0);
   Notification notification = new Notification.Builder(this)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setAutoCancel(true)//自动关闭
                .setContentTitle("悬挂式通知")
                .setContentText("您有一项新活动")
                .setChannelId("003")
                .setPriority(Notification.PRIORITY_HIGH)//设置该通知优先级
                .setAutoCancel(true)//自动取消
                .setDefaults(Notification.DEFAULT_VIBRATE)//默认设置震动
                .build();
    //NotificationChannel需要设置优先级IMPORTANCE_HIGH
    NotificationChannel channel = new NotificationChannel("003","悬挂式通知",NotificationManager.IMPORTANCE_HIGH);
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);
    manager.notify(1,notification);

提示:悬挂式通知需要在手机里开启悬浮通知权限


总结

以上就是今天要讲的内容,本文仅仅简单介绍了Notification的使用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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