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();
  • 相关阅读:
    【Leetcode】328.奇偶链表
    【Leetcode】127.单词接龙(BFS与DFS区别)
    从ReentrantLock加锁解锁角度分析AQS
    一文解决LeetCode岛屿问题
    IIS 解决首次加载慢的问题
    IEqualityComparer<TSource> 比较规则
    C# 闭包问题 (待完善)
    两个MD5值一样的 128 byte sequences
    Windows解决忘记用户密码
    部署在阿里云上的项目收到了阿里云发送的shiro漏洞
  • 原文地址:https://www.cnblogs.com/shineyoung/p/11578478.html
Copyright © 2011-2022 走看看