Notification集合(二)

这两天公司项目用到消息推送机制,想将后台推送的消息以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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值