打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android Notification

先看老版本的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,用来区别不同的notificationmNM.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();

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android Notification 详解
全面解析Notification
android NotificationCompat.Builder 使用
全面了解Android Notification
Android O中Notification 的修改与其使用错误
Android Service解析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服