android中的通知Notification

1、Notification的作用
   用于通知用户关于APP的一些信息。(例如:推送的聊天消息,升级消息等等)
  2、Notification的用法
   a、
    //创建NotificationBuilder
           //创建PendingIntent
           //将PendingIntent设置到NotificationBuilder中

           //创建Notification
           //创建RemoteViews
           //将RemoteViews设置到Notification的contentView中
           //通过NotificationManager的notify方法,发送通知。
   b、Flags属性:
    a、FLAG_AUTO_CANCEL:用户点击这个消息,这个消息便消失。
    b、FALG_NO_CLEAR:只有全部清空消息时,这个消息才消失。
    c、FLAG_SHOW_LIGHTS:三色灯提示,在使用三色灯时,必须加上该标记。
    d、FLAG_INSISTENT:让声音,震动,无限循环,直到用户操作。或者代码取消。
    e、FLAG_ONLY_ALERT_ONCE:发起通知后,铃声和震动只执行1次。
    f、FLAG_ONGOING_EVENT:发起通知正在运行的事件。

3、默认通知的创建步骤:
1、Notification创建,通过系统服务创建
2、创建Notification Builder
3、创建 Builder
4、创建PendingIntent
5、通过NotificationManager来唤醒提交通知

代码:

  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
public void showNotification01(NotificationManager notificationManager) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1000,
                new Intent(this, FromNotificationActivity.class),
                PendingIntent.FLAG_UPDATE_CURRENT);
        //创建Notification.Builder
        Notification.Builder builder = new Notification.Builder(this);
        //配置Builder属性
        //设置通知栏图案
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置通知栏缩略内容
        builder.setTicker("XXX发来一条语音消息,请注意查收!");
        builder.setContentTitle("Notification ID: " + ID_NOTIFICATION);
        builder.setContentText("XXX巴拉巴拉说了一堆详细内容");
        builder.setNumber(1);
        builder.setContentIntent(pendingIntent);

        Notification notification = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification = builder.build();

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            notification = builder.getNotification();
        } else {
            Toast.makeText(this, "您的手机太破了,扔了吧!", Toast.LENGTH_SHORT).show();
        }
        notification.flags = Notification.FLAG_NO_CLEAR;
        notificationManager.notify(ID_NOTIFICATION, notification);

    }

2、创建自定义通知:

   public void showNotification02(NotificationManager notificationManager) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1000, 
                new Intent(this, FromNotificationActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.icon);
        //设置通知栏缩略内容
        builder.setTicker("'这是一条自定义通知消息,请查看!");
        builder.setContentTitle("Notification ID: " + ID_NOTIFICATION);
        builder.setContentText("XXX巴拉巴拉说了一堆详细内容");
        builder.setNumber(1);
        builder.setContentIntent(pendingIntent);


        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification = builder.build();

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            notification = builder.getNotification();
        } else {
            Toast.makeText(this, "您的手机太破了,扔了吧!", Toast.LENGTH_SHORT).show();
        }
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
        remoteViews.setTextViewText(R.id.textview_title, "'英国成功脱欧");
        remoteViews.setTextViewText(R.id.textview_content, "'英国不仅成功脱欧,欧洲杯 英格兰VS冰岛 1:2 ");
        remoteViews.setImageViewResource(R.id.imageview_icon, R.drawable.icon);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification.bigContentView = remoteViews;

        } else {
            notification.contentView = remoteViews;
        }

        notification.contentView = remoteViews;
        notification.flags = Notification.FLAG_NO_CLEAR;
        notificationManager.notify(ID_NOTIFICATION, notification);

    }



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值