这两天公司项目用到消息推送机制,想将后台推送的消息以notification的形式展示个用户,自己也就试着这了几个小demo(上一篇博客介绍的是普通的notification,这篇介绍的是几个复杂的notification,部分注释不是很完整,具体参考我的上一篇博客 Notification集合(一),.这里就不累赘了),废话不多说,直接上代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button normalView;
private Button bigview_text;
private Button bigview_picture;
private Button bigview_inbox;
private static final int NOTIFICATION_ID_NV = 1;
private static final int NOTIFICATION_ID_BT = 2;
private static final int NOTIFICATION_ID_BP = 3;
private static final int NOTIFICATION_ID_BI = 4;
private Bitmap bigIcon;
private Bitmap bigPicture;
private int messageNum = 1;
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
initDate();
}
private void initView() {
normalView = (Button) findViewById(R.id.normalview);
bigview_text = (Button) findViewById(R.id.bigview_text);
bigview_picture = (Button) findViewById(R.id.bigview_picture);
bigview_inbox = (Button) findViewById(R.id.bigview_inbox);
}
private void initListener() {
normalView.setOnClickListener(this);
bigview_text.setOnClickListener(this);
bigview_picture.setOnClickListener(this);
bigview_inbox.setOnClickListener(this);
}
private void initDate() {
bigIcon = BitmapFactory.decodeResource(this.getResources(), R.mipmap.notify_icon_big);
bigPicture = BitmapFactory.decodeResource(getResources(), R.mipmap.bigpicture);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.normalview:
showNormal();
break;
case R.id.bigview_text:
showBigViewText();
break;
case R.id.bigview_picture:
showBigViewPic();
break;
case R.id.bigview_inbox:
shoewBigViewInbox();
break;
default:
break;
}
}
/**
* 显示正常的notification
*/
private void showNormal() {
Notification notify = new NotificationCompat.Builder(this)
.setLargeIcon(bigIcon)
.setSmallIcon(R.mipmap.notify_icon_small)
.setTicker("normal来了")
.setContentInfo("normal content info")
.setContentTitle("normal title")
.setContentText("normal text")
.setNumber(messageNum++)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.build();
manager.notify(NOTIFICATION_ID_NV, notify);
}
/**
* 内容部分显示多行文本
*/
private void showBigViewText() {
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();//获取notification的style模式
Intent intent = new Intent(this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
bigTextStyle
.setBigContentTitle("style content title")//如果不设置这条,标题部分则显示notification中设置的contentTitle;
.setSummaryText(" style summary text")
.bigText("I am bit text I am bit text I am bit text I am bit text I am bit text I am bit text I am bit text I am bit text I am bit text I am bit text I am bit text ");
Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(bigIcon)
.setSmallIcon(R.mipmap.notify_icon_small)
.setTicker("bigViewText来了")
.setContentInfo("bigviewtext content info")
.setContentTitle("bigviewtext title")
//.setContentText("bigviewtext text")//这一条舌部设置没什么用
//.setNumber(messageNum++)//如果设置contentInfo,则被contentInfo覆盖
.setAutoCancel(true)
.setStyle(bigTextStyle)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.build();
manager.notify(NOTIFICATION_ID_BT, notification);
}
/**
* notification下面显示一个大图片
*/
private void showBigViewPic() {
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setSummaryText("style big summary text")
.setBigContentTitle("style big conntent title")
.bigPicture(bigPicture);
Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(bigIcon).setSmallIcon(R.mipmap.notify_icon_small)
.setTicker("bitviewpic 来了").setContentInfo("bigviewpic content info")
.setContentText("bigviewpic content text")
.setAutoCancel(true)
.setStyle(bigPictureStyle)
.setDefaults(Notification.DEFAULT_ALL)
.build();
manager.notify(NOTIFICATION_ID_BP, notification);
}
/**
* 显示多行文本(个人认为与showBigViewText区别不大,只是这个有个自动换行)
*/
private void shoewBigViewInbox() {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("style bigcontent title").setSummaryText("style summary text");
for (int i = 0; i < 5; i++) {
inboxStyle.addLine("newline:" + i);
}
Notification notification = new NotificationCompat.Builder(this).setLargeIcon(bigIcon).setSmallIcon(R.mipmap.notify_icon_small)
.setTicker("bigview inbox 来了").setContentInfo("inbox info")
.setContentText("bigvie inbox content").setStyle(inboxStyle)
.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.build();
manager.notify(NOTIFICATION_ID_BI, notification);
}
}