热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android8.0实现发送通知

这篇文章主要为大家详细介绍了Android8.0实现发送通知,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在Android8.0以后,针对Notification 通知api做了修改,新增了通知渠道(NotificationCannel)。下面就把demo的详细代码记录下:

1.Application 为NotificationManager添加通知频道

import android.app.Application;

import com.xinrui.ndkapp.notification.NotificationChannels;

public class NdkApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    NotificationChannels.createAllNotificationChannels(this);
  }
}

2.NotificationChannels 类

public class NotificationChannels {
  public final static String CRITICAL = "critical";
  public final static String IMPORTANCE = "importance";
  public final static String DEFAULT = "default";
  public final static String LOW = "low";
  public final static String MEDIA = "media";

  public static void createAllNotificationChannels(Context context) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if(nm == null) {
      return;
    }

    NotificationChannel mediaChannel = new NotificationChannel(
        MEDIA,
        context.getString(R.string.app_name),
        NotificationManager.IMPORTANCE_DEFAULT);
    mediaChannel.setSound(null,null);
    mediaChannel.setVibrationPattern(null);

    nm.createNotificationChannels(Arrays.asList(
        new NotificationChannel(
            CRITICAL,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_HIGH),
        new NotificationChannel(
            IMPORTANCE,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_DEFAULT),
        new NotificationChannel(
            DEFAULT,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_LOW),
        new NotificationChannel(
            LOW,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_MIN),
        //custom notification channel
        mediaChannel
    ));
  }
}

3.发送通知

public void sendSimpleNotification(Context context, NotificationManager nm) {
    //创建点击通知时发送的广播
    Intent intent = new Intent(context, NotificationMonitorService.class);
    intent.setAction("android.service.notification.NotificationListenerService");
    PendingIntent pi = PendingIntent.getService(context,0,intent,0);
    //创建删除通知时发送的广播
    Intent deleteIntent = new Intent(context,NotificationMonitorService.class);
    deleteIntent.setAction(Intent.ACTION_DELETE);
    PendingIntent deletePendingIntent = PendingIntent.getService(context,0,deleteIntent,0);
    //创建通知
    Notification.Builder nb = new Notification.Builder(context, NotificationChannels.DEFAULT)
        //设置通知左侧的小图标
        .setSmallIcon(R.drawable.ic_notification)
        //设置通知标题
        .setContentTitle("Simple notification")
        //设置通知内容
        .setContentText("Demo for simple notification!")
        //设置点击通知后自动删除通知
        .setAutoCancel(true)
        //设置显示通知时间
        .setShowWhen(true)
        //设置通知右侧的大图标
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
        //设置点击通知时的响应事件
        .setContentIntent(pi)
        //设置删除通知时的响应事件
        .setDeleteIntent(deletePendingIntent);
    //发送通知
    nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build());
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • PDFtoWordConverter介绍PDFtoWordConverter‘pdftowordconverter’isatoolwhichhelpstoconvertpdffor ... [详细]
  • CephPool资源池管理#查看ceph资源池cephosdlspools#创建资源池osdpoolcreate{}{rep ... [详细]
  • Graylog是与ELK可以相提并论的一款集中式日志管理方案,支持数据收集、检索、可视化Dashboard。本节将实践用Graylog来管理Docker日志。Graylog架构Gr ... [详细]
  • 前期部署1.JDK安装,配置PATH2.下载spark-1.6.1-bin-hadoop2.6.tgz,并上传到服务器解压3.在 usr 下创建软链接到目标文件夹4.修改配置文件, ... [详细]
  • mac用于开发使用时间长硬盘会越来越小,速度越来越慢的,亦是花了几分钟研究怎么清理系统的缓存,方法:1,到https:www.omnigroup.commore安装OmniDisk ... [详细]
  • 点击elementui表格中的图标,上方显示具体的文字描述
     <template><el-ta ... [详细]
  • 又给自己挖了一个坑跳进去。KafkaManager使用单例模型获取到一个producer,然而自己代码里用的时候加了一个using然后自己在做测试的时候,for循环加10条数据发送 ... [详细]
  • 设计模式(一)—— 策略模式
    简述:策略模式的适用的目标是多子类和单一父类的情形。父类中放的是很多子类共用的代码段,对于不同子类特殊的代码段交给子类进行编写。但如果两个或两个以上的子类需要共同的代码段时,不能将 ... [详细]
  • 大学没好好读书,那会没怎么明白冒泡排序是这么回事早上睡到九点多起来,就在房间看书、听歌,下午吃完饭做了下冒泡排序,现在把代码贡献如下:其实还可以改良的,节省时间空间,有时 ... [详细]
  • Emgu 学习之HelloWorld
    安装和配置系统Win10,VS2013,下载Emgu安装包libemgucv-windesktop-3.4.3.3016安装到了E:\OpenCV\emgucv-windeskto ... [详细]
  • EL&&JSTL
    EL表达式概念ExpressionLanguage表达式语言。作用替换和简化jsp页面中java代码的编写语法${表达式}注意jsp默认支持el表达式的。如果要忽略el表达式可以使 ... [详细]
  • Django信号使得某个操作之前能定制化一些任务-内置信号-导入fromdjango.core.signalsimportXX00-注册函数-自定义-自定义-定义信号importd ... [详细]
  • HDU 3487 Play with Chain
    题意:对序列取出连续的一段接到剩下的第k个值后面,或者把一段序列反转。解题思路:splay区间操作。解题代码:1FileName:hdu3487.cpp2Author:darkdr ... [详细]
  • svnstat查看当前目录下svn状态svnremovexxxxsvnaddxx ... [详细]
  • 在Windows下配置安装OMNeT++ 4.0
    在Windows下配置安装OMN ... [详细]
author-avatar
chung
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有