您的位置:首页 > 移动开发 > Android开发

android中的notification

2016-03-31 14:25 489 查看
在手机使用中我们常常会接到应用程序向用户发送的通知,在android中 我们用notification这个装载信息发送给用户, 一般叫它通知, 而管理通知时(比如发送和取消)要用NotificationManager这个类.

用户在接到通知时,状态栏上会有通知的小图标,同时有接到的通知音效等,向下拉时展示具体的内容.

接下来介绍一下通知的用法和两种视觉效果.

在介绍之前做一些准备工作:

private Button sendButton, cancelButton,bigPictureButton,inboxStyleButton;
private NotificationManager manager;
private int NOTIFICATION_ID = 2002;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

sendButton = (Button) findViewById(R.id.send_button);
cancelButton = (Button) findViewById(R.id.cancel_button);
bigPictureButton = (Button) findViewById(R.id.big_pic);
inboxStyleButton = (Button) findViewById(R.id.inbox);

sendButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
bigPictureButton.setOnClickListener(this);
inboxStyleButton.setOnClickListener(this);

}


1.notification的构建方法

在API4之前 notification的构建方法推荐用Notification.Builder

public void onClick(View v) {
switch (v.getId()) {
case R.id.send_button:
Bitmap largeIcon0 = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
//3.0之前的版本
Notification notification = new Notification.Builder(this)
.setContentTitle("memmememe")
.setContentText("lallalallal")
.setLargeIcon(largeIcon0)
.setSmallIcon(R.mipmap.ic_launcher)
//设置默认的声音等
.setDefaults(Notification.DEFAULT_ALL)
//设置时间
.setWhen(System.currentTimeMillis())
//3.0前可以设置未读消息有多少条
.setNumber(13)
//点击后自己消失
.setAutoCancel(true).build();
manager.notify(NOTIFICATION_ID, notificationCompat.build());
break;


在notification中必须要设置的属性:

// 1.contentTitle 通知栏消息标题

// 2.contentText 通知栏消息内容

// 3.smallIcon 小的icon



在这里我们可以看出,除了需要要设置的属性, 还有几个属性也非常重要, 比如setWhen , setTicker ,setAutoCancel等等,这里有一个setcontentIntent,稍后再说.

在设置完notification后,manager调用notify()的方法将通知发送出去,其中的两个参数一个是notification的ID 做为消息的标识,是开发者设置的,另一个就是刚刚构建好的notification对象,至此 我们就能完整的发送一个通知了.

如果想取消通知 怎么办呢?

case R.id.cancel_button:
manager.cancel(NOTIFICATION_ID);
break;


在API4之后,notification的构建方法推荐使用NotificationCompat.Builder

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(this);
notificationCompat.setContentTitle("mememem")
.setContentText("lallala")
.setLargeIcon(largeIcon)
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notificationCompat.build());


以上两种构建方法 , 都是标准视图,在android中是通用的,但在android4.1之后,compat可以设置notification的style,有三种style分别是:NotificationCompat.BigPictureStyle NotificationCompat.BigTextStyle NotificationCompat.InboxStyle

在API中有详细的用法介绍:



其他两种就不介绍了,API里写的很明白



在消息过程中,还有一个重要的类用来实现页面跳转,PendingIntent 是未决定的intent ,

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


pendingIntent在跳转时,有三种获取方式


1、getActivity(Context, int, Intent, int),跳转到一个activity组件


2、getBroadcast(Context, int, Intent, int),打开一个广播组件



3、getService(Context, int, Intent, int),打开一个服务组件


具体的用法:

NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(this);
notificationCompat.setContentTitle("mememem")
.setContentText("lallala")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notificationCompat.build());


通过setContentIntent()方法,可以设置点击通知后跳转的方式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: