分享

Android Notification

 wwwijhyt图书馆 2020-07-11

先看老版本的Notification该如何使用

复制代码
Notification notification = new Notification.Builder(this)
                .setSmallIcon(moodId)//小图标
                .setWhen(System.currentTimeMillis())//时间
                .setContentTitle(getText(R.string.status_bar_notifications_mood_title))//标题
                .setContentText(text)  // the contents of the entry
                .setContentIntent(contentIntent)  //The PendingIntent send when the entry is clicked
                .build();
复制代码

new了一个notification之后,我们还需要发送出去,有时候我们还要取消发送出去的notification

所以,我们需要NotificationManger

mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//MOOD_NOTIFICATIONS是通知id,用来区别不同的notification
mNM.notify(MOOD_NOTIFICATIONS, notification);

mNM.cancel(MOOD_NOTIFICATIONS);
mNM.cancelAll() //取消之前所有的通知

所以一个通知发送到通知栏的简单流程是这样的 new一个Notification,使用NotificationManger发送出去

下面详细讲解new Notification是需要设置的参数

必要参数

  • 小图标,由 setSmallIcon() 设置
  • 标题,由 setContentTitle() 设置
  • 详细文本,由 setContentText() 设置

其他参数

  • 时间,setWhen(System.currentTimeMillis())

  • PendingIntent,一般用来打开Activity,setContentIntent(contentIntent) 

  • 等等

 在Android8以后,Android新增了“渠道”,所有通知必需要添加渠道才能发送

原有代码适配的简单流程,new一个channel,setChannel

 

复制代码
String id = "channel_001";
String name = "name";

NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
//创建通知时设置渠道
notification = new Notification.Builder(context)
            .setChannelId(id)
            .setContentTitle("活动")
            .setContentText("您有一项新活动")
            .setSmallIcon(R.drawable.app_logo).build();
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多