Android--Notification横幅通知栏

本文详细介绍了Android中的Notification,包括其全局效果、属性设置、创建通知通道、发出通知、PendingIntent的使用、自定义通知栏、显示多条通知、消息数显示以及如何监听通知点击事件。同时提到了Foreground Service在保活中的应用,以及如何控制展开/折叠通知栏和实现通知跳转到指定页面。
摘要由CSDN通过智能技术生成

一、概述

1、Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。在发出一条通知后,手机最上方的状态栏会显示一个通知的图标,下拉状态栏后就可以看到通知的详细内容。

2、样式

其属性描述如下:

1.代表: 通知标题,通过 setContentTitle() 方法设置。
2.代表: 大图标,通过 setSmallIcon() 方法设置。
3.代表:  通知内容,通过 setContentText() 方法设置。 
4.代表: 通知消息 
5.代表:小图标,通过 setLargeIcon() 方法设置。
6.代表: 通知时间,一般为系统时间,也可以使用setWhen()设置。

当 setSmallIcon() 与 setLargeIcon() 同时存在时, smallIcon 显示在通知的右下角, largeIcon 显示在左侧;当只设置 setSmallIcon() 时, smallIcon 显示在左侧。

二、基本用法

1、创建通道

NotificationChannel是android8.0新增的特性,如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    //android 8.0以上需要特殊处理,也就是targetSDKVersion为26以上
    createNotificationChannel();
}

@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
    channel.canBypassDnd();//是否绕过请勿打扰模式
    channel.enableLights(true);//闪光灯
    channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
    channel.setLightColor(Color.RED);//闪关灯的灯光颜色
    channel.canShowBadge();//桌面launcher的消息角标
    channel.enableVibration(true);//是否允许震动
    channel.getAudioAttributes();//获取系统通知响铃声音的配置
    channel.getGroup
要实现 FCM 自定义通知横幅,你需要在客户端和服务器端分别进行以下配置: 客户端配置: 1. 在你的项目级 build.gradle 文件中添加以下依赖: ``` implementation 'com.google.firebase:firebase-messaging:22.0.0' ``` 2. 在你的应用级 build.gradle 文件中添加以下配置: ``` android { // ... defaultConfig { // ... // 设置通道 ID,用于兼容 Android 8.0 及以上版本的通知 notificationChannelId "my_channel_id" } } // 在 AndroidManifest.xml 文件中添加以下权限和服务声明 <uses-permission android:name="android.permission.INTERNET" /> <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> ``` 3. 创建一个继承自 FirebaseMessagingService 的服务类,并重写 onMessageReceived 方法,用于处理接收到的消息,如下所示: ``` public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMessagingService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); // 发送自定义横幅通知 sendCustomNotification(remoteMessage); } } private void sendCustomNotification(RemoteMessage remoteMessage) { // 创建一个 NotificationCompat.Builder 对象 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle(remoteMessage.getNotification().getTitle()) .setContentText(remoteMessage.getNotification().getBody()) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true); // 添加自定义横幅样式 NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle() .setBigContentTitle(remoteMessage.getNotification().getTitle()) .bigText(remoteMessage.getNotification().getBody()); builder.setStyle(bigTextStyle); // 显示通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(1, builder.build()); } } ``` 服务器端配置: 1. 使用 Firebase 控制台创建一个新的 Firebase 项目,并在项目设置中获取到项目的 Server key。 2. 使用项目的 Server key 发送消息到 FCM 接口,消息格式如下: ``` { "to": "设备的 FCM token", "notification": { "title": "通知标题", "body": "通知内容" }, "data": { // 自定义数据 } } ``` 这样,当你的应用接收到消息时,就会发送一个自定义横幅通知。需要注意的是,自定义横幅样式只会在 Android 5.0 及以上版本的设备上生效。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值