zoukankan      html  css  js  c++  java
  • Android Notification

    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,用来区别不同的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();
  • 相关阅读:
    函数参数太多的一种简化方法
    laravel 获取所有表名
    使用 laravel 的 queue 必须知道的一些事
    git "refusing to merge unrelated histories" 解决方法
    使用 xhprof 进行 php 的性能分析
    php 性能优化
    js原生实现轮播图效果(面向对象编程)
    nextSibling 和nextElementSibling
    如何在页面中使用svg图标
    svg动画 animate
  • 原文地址:https://www.cnblogs.com/shineyoung/p/11578478.html
Copyright © 2011-2022 走看看