Notification
Notification,通知。Android 里的通知设置无非下面几步:
- 创建一个 NotificationCompat.Builder 对象,并对其进行设置;
- 创建 Notification 对象,并对其进行设置;
- 创建 NotificationManager 对象;
- 使用 NotificationManager 的 notify 方法来发送通知。
Notification 里的相关概念
直接上图,图片最直观


SmallIcon: 显示在状态栏,通知的图标
Ticker: 显示在状态栏,通知的提示文字(注意,Ticker 在 5.0 及以上的版本是不显示的,具体请看这篇博客 安卓 Notification 使用方法小结)
LargeIcon: 显示在下拉通知栏,通知的图标,为 Bitmap 类型,不设置时默认使用 SmallIcon 的图标
ContentTitle: 显示在下拉通知栏,通知的标题
ContentText: 显示在下拉通知栏,通知的内容
普通的通知
通知的最基本方法
1. XML 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.monkeychan.notificationtest.MainActivity" >
<Button
android:id="@+id/base_notification"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="简单的通知"
android:onClick="notificationClick"/>
</LinearLayout>
2. Java 代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationClick(View view) {
// 1. 创建一个 NotificationCompat.Builder 对象,并对其进行设置
NotificationCompat.Builder builder = new Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher) // 设置通知的图标
.setTicker("马云发来一条消息").setContentTitle("发财秘笈") // 设置标题的标题
.setContentText("想发财吗?洗洗睡吧!啊哈哈哈哈"); // 设置的标题内容
// 2. 创建一个 Notification 对象,并对其进行设置
// 注意:当 NotificationCompat.Builder 对象设置完成之后才创建,
// 否则会导致之后 NotificationCompat.Builder 对象的设置无效
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_ALL; // 设置通知震动和声音为默认
// 3. 创建一个 NotificationManager 对象来获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 4. 使用 NotificationManager 的 notify 方法来发送通知
manager.notify(101, notification);
}
}
3. 效果演示(基于 4.2.2 虚拟机演示):


带跳转的通知
点击通知后能跳转到某个界面
基本上与普通的通知一样,只是 Java 代码部分做了一点修改。
1. XML 布局文件:
- 主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.monkeychan.notificationtest.MainActivity" >
<Button
android:id="@+id/intent_notification"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="带跳转的通知"
android:onClick="notificationClick"/>
</LinearLayout>
- 主界面的的 Java 代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationClick(View view) {
// 1. 创建一个 NotificationCompat.Builder 对象,并对其进行设置
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher) // 设置通知的图标
.setTicker("马云发来一条消息") // 由于我是在 5.0 以上的机子演示,所以此处无效
.setContentTitle("发财秘笈") // 设置标题的标题
.setContentText("想发财吗?赶紧点我吧!"); // 设置的标题内容
// 通知中绑定跳转的 Activity 的具体步骤:
//1) 创建一个 Intent 对象,声明我们要跳转到的Activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//2) 创建一个 PendingIntent 对象
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//3) 调用 setContentIntent 方法,将通知与 PendingIntent 绑定起来
builder.setContentIntent(pendingIntent);
// 2. 创建一个 Notification 对象,并对其进行设置
// 注意:当 NotificationCompat.Builder 对象设置完成之后才创建,
// 否则会导致之后 NotificationCompat.Builder 对象的设置无效
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_ALL; // 设置通知震动和声音为默认
// 3. 创建一个 NotificationManager 对象来获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 4. 使用 NotificationManager 的 notify 方法来发送通知
manager.notify(102, notification);
}
}
上面关于 PendingIntent 的部分,具体可参考这里 Intent和PendingIntent的区别、Android:PendingIntent的FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT和Android中pendingIntent的深入理解
- 然后是跳转后的界面的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="还是洗洗睡吧!啊哈哈哈哈!"
android:textSize="24sp"
android:textColor="#FF4500"/>
</RelativeLayout>
- 跳转后的界面的 Java 代码:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("跳转后的界面"); // 设置 label 标签的标题
}
}
- 最后,别忘了在 AndroidManifest.xml 文件里注册 Activity!!!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.monkeychan.notificationtest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 不要忘了注册!!! -->
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
- 效果演示:
- 状态栏:

从上面可以看到,状态栏并不显示 Ticker。
- 下拉通知栏:

- 点击通知之后跳转:

大视图的通知
大视图的通知有三种类型,分别是 NotificationCompat.InboxStyle,NotificationCompat.BigTextStyle 和 NotificationCompat.BigPictureStyle。这里介绍的是 NotificationCompat.BigPictureStyle。和普通的通知基本步骤一样,只是 Java 代码有点修改而已。
1. XML 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.monkeychan.notificationtest.MainActivity" >
<Button
android:id="@+id/bitmap_notification"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="大视图的通知"
android:onClick="notificationClick"/>
</LinearLayout>
2. Java 代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationClick(View view) {
// 1. 创建一个 NotificationCompat.Builder 对象,并对其进行设置
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher) // 设置通知的图标
.setTicker("马云发来一条消息") // 由于我是在 5.0 以上的机子演示,所以此处无效
.setContentTitle("发财秘笈") // 设置标题的标题
.setContentText("想发财吗?赶紧点我吧!"); // 设置的标题内容
// 创建大视图通知的步骤:
// 1) 创建一个 Bitmap 对象
Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
// 2) 创建一个 NotificationCompat.BigPictureStyle 对象
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitMap);
// 3) 设置大视图样式
builder.setStyle(bigPictureStyle);
// 2. 创建一个 Notification 对象,并对其进行设置
// 注意:当 NotificationCompat.Builder 对象设置完成之后才创建,
// 否则会导致之后 NotificationCompat.Builder 对象的设置无效
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_ALL; // 设置通知震动和声音为默认
// 3. 创建一个 NotificationManager 对象来获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 4. 使用 NotificationManager 的 notify 方法来发送通知
manager.notify(103, notification);
}
}
3. 效果演示:
- 主界面:

- 下拉通知栏:

带进度条的通知
常用于文件的下载,在状态栏显示文件下载进度。
1. XML 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.monkeychan.notificationtest.MainActivity" >
<Button
android:id="@+id/progress_notification"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="带进度条的通知"
android:onClick="notificationClick"/>
</LinearLayout>
2. Java 代码:
package net.monkeychan.notificationtest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationClick(View view) {
// 1. 创建一个 NotificationCompat.Builder 对象,并对其进行设置
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher) // 设置通知的图标
.setContentTitle("文件下载") // 设置标题的标题
.setContentText("文件下载中..."); // 设置的标题内容
// 创建带进度条的通知的步骤:
// 设置进度条:
// 第一个参数为最大值,
// 第二个参数为当前进度,
// 第三个参数表示不确定性,如果为true,则进度不确定,会显示动画,
// 如果为false,就按照我们设定的进度显示,不会显示
builder.setProgress(100, 50, true);
builder.setOngoing(true); // 设置是否为进行中的通知,为 true 时不可清除,为 false 时可以清除
// 2. 创建一个 Notification 对象,并对其进行设置
// 注意:当 NotificationCompat.Builder 对象设置完成之后才创建,
// 否则会导致之后 NotificationCompat.Builder 对象的设置无效
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_ALL; // 设置通知震动和声音为默认
// 3. 创建一个 NotificationManager 对象来获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 4. 使用 NotificationManager 的 notify 方法来发送通知
manager.notify(104, notification);
}
}
3. 效果演示:
- 主界面:

- 下拉通知栏:

自定义的通知
即使用我们的自定义布局。
1. XML 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.monkeychan.notificationtest.MainActivity" >
<Button
android:id="@+id/custom_notification"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="自定义的通知"
android:onClick="notificationClick"/>
</LinearLayout>
2. 自定义布局的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:text="自定义通知"
android:textSize="16sp"
android:textColor="#FF4500"/>
</LinearLayout>
3. Java 代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notificationClick(View view) {
// 1. 创建一个 NotificationCompat.Builder 对象,并对其进行设置
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher);
// 创建带自定义通知的步骤:
// 创建一个 RemoteViews 对象:
// 第一个参数为当前包名,
// 第二个参数为要传入的自定义布局,
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom);
// 2. 创建一个 Notification 对象,并对其进行设置
// 注意:当 NotificationCompat.Builder 对象设置完成之后才创建,
// 否则会导致之后 NotificationCompat.Builder 对象的设置无效
Notification notification = builder.build();
notification.contentView = rv; // 调用 Notification 对象的 contentView 方法来传入自定义的布局
notification.defaults = Notification.DEFAULT_ALL; // 设置通知震动和声音为默认
// 3. 创建一个 NotificationManager 对象来获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 4. 使用 NotificationManager 的 notify 方法来发送通知
manager.notify(105, notification);
}
}
4. 效果演示:
- 主界面:

- 下拉通知栏:

总结
其实 Notification 的设置步骤都差不多,只是某些方面有些许差别,整体还是一样的。当然,以上都是简单的,Notification 还有更多的用法,尤其是自定义这部分。
参考资料:
网友评论