“我没有很聪明,也不是那么努力,我只是有点不服输”
0x01.简介
A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system controlled areas that the user can view at any time.
--引用谷歌关于Notification的描述
Notification是一个将你程序的消息展示在程序之外的UI控件,当你告诉系统去产生一个Notification时,它会在Notification area出现一个图标,你可以打开Notification drawer去看详细的信息。Notification are 和Notification drawer都是系统控制的区域,用户可以随时看到。
Notification不仅仅用于显示一则消息,也可以用于和用户进行交互。比如音乐播放,它的Notification具有音乐控制功能,并且在android7.0(api level 24)上Notification可以直接通过Notification进行回复,比如有一则微信消息,你可以通过下拉框直接回复啦,不用通过点进去再回复了,我想在Android7.0系统上,微信和QQ应该会这么做。效果图如图1.0(引用谷歌官网)。
图1.0
0X02.最简单的Notification
- setContentTitle() 设置Notification标题
- setContentText() 设置Notification内容
- setSmallIcon() 设置Notification小图标(对,还有setLargeIcon不同图片用于不同的设备)
NotificationCompat.Builder build = new NotificationCompat.Builder(this);//定义一个创建Notification的build
build.setContentTitle("this is title").setContentText("this is text").setSmallIcon(R.mipmap.ic_launcher);//为Notification设置所需要的属性
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统Notification服务
manager.notify(0, build.build());//显示
0X03.结合PendingIntent的Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);//创建build对象
builder.setContentTitle("this is title").setContentText("this is text").setSmallIcon(R.mipmap.ic_launcher);//设置必须的属性
Intent notifyIntent = new Intent(this, SpecialNotificationActivity.class);//创建用于跳转的Intent
PendingIntent notifyPendingIntent = PendingIntent.getActivity( this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);//获得可以在别的应用可以用的Intent,别名PendingIntent。
builder.setContentIntent(notifyPendingIntent);//设置外部应用需要用的PendingIntent
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//获取系统的Notification服务
mNotificationManager.notify(0, builder.build());//显示这个Notification
PendingIntent是在Intent的上面进行进一步的封装,getActivity(Context context, int requestCode, Intent intent, int flag)可以获得一个PendingIntent。我们看下具体的参数
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//获取系统的Notification服务
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("this is title1").setContentText("this is special").setSmallIcon(R.mipmap.ic_launcher);//设置必须的属性
Intent intent1 = new Intent(this, Activity1.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent2);
notificationManager.notify(1, builder.build());
<span style="white-space:pre"> </span>//下面我们增加一个Notification,ID不相同,但是生成的PendingIntent的requestCode,并且把Flag设成FLAG_UPDATE_CURRENT.
Intent intent2 = new Intent(this, Activity2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle("this is title 2");//显示不同的标题
builder.setContentIntent(pendingIntent);
notificationManager.notify(2, builder.build());//ID不同就会显示两个Notification
一开始我们为title1设置的是跳转到Activity1,为title2设置的是跳转到Activity2。但是实际结果是点击两个都会跳转到Activity2,这就是requestCode和flag共同下的作用,当第二个PendingIntent传入Notification时,flag设置成FLAG_UPDATE_CURRENT,会更新所有requestCode和它相同的PendingIntent,所以Title1中的也被更新了。有位很详细的介绍了PendingIntent的requestCode参数
戳这查看。
0X04.自定义的Notification
NotificationCompat.Builder build = new NotificationCompat.Builder(this);//定义一个创建Notification的build
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Main2Activity.class), PendingIntent.FLAG_UPDATE_CURRENT);//获得PendingIntent
RemoteViews rm = new RemoteViews(getPackageName(), R.layout.notification_custom);//生成RemoteView
rm.setOnClickPendingIntent(R.id.press_here_button, pi);//为其中一个按钮设置点击事件
build.setContent(rm);//设置RemoteVIew
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统Notification服务
manager.notify(0, build.build());//通知显示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is custom layout"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is second line"/>
<Button
android:id="@+id/press_here_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="press here"/>
</LinearLayout>