android里面经常会使用Notification来显示通知的消息,一般使用NotificationManager来创建通知消息

 NotificationManager manger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//为了版本兼容 选择V7包下的NotificationCompat进行构造
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//Ticker是状态栏显示的提示
builder.setTicker("1");
//第一行内容 通常作为通知栏标题
builder.setContentTitle("推送标题");
//第二行内容 通常是通知正文
builder.setContentText("内容");
//可以点击通知栏的删除按钮删除
builder.setAutoCancel(true);
//系统状态栏显示的小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.ti);
builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_LIGHTS);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Intent clickIntent = new Intent(); //点击通知之后要发送的广播
// int id = (int) (System.currentTimeMillis() / 1000);
// clickIntent.addCategory(context.getPackageName());
// clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED);
// clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, bundle.getString(JPushInterface.EXTRA_EXTRA));
// PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// notification.contentIntent = contentIntent;
manger.notify(1, notification);

  然而在Android8.0以上的版本并不能看到通知的内容,android8.0需要使用NotificationChannel来处理通知的显示,根据处理得到了以下内容。

package com.snail.monitor.util;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat; import com.snail.monitor.R; import cn.jpush.android.api.JPushInterface; import static android.app.Notification.VISIBILITY_SECRET;
import static android.support.v4.app.NotificationCompat.PRIORITY_DEFAULT; /**
* Created by zd on 2019/1/7.
*/ public class NotificationUtils extends ContextWrapper { public static final String CHANNEL_ID = "default";
private static final String CHANNEL_NAME = "Default Channel";
private static final String CHANNEL_DESCRIPTION = "this is default channel!";
private NotificationManager mManager; private Context context;
private String EXTRA_EXTRA;
public NotificationUtils(Context base,String EXTRA_EXTRA) {
super(base);
context = base;
this.EXTRA_EXTRA = EXTRA_EXTRA;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
} @TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
//是否绕过请勿打扰模式
channel.canBypassDnd();
//闪光灯
channel.enableLights(true);
//锁屏显示通知
channel.setLockscreenVisibility(VISIBILITY_SECRET);
//闪关灯的灯光颜色
channel.setLightColor(Color.RED);
//桌面launcher的消息角标
channel.canShowBadge();
//是否允许震动
channel.enableVibration(true);
//获取系统通知响铃声音的配置
channel.getAudioAttributes();
//获取通知取到组
channel.getGroup();
//设置可绕过 请勿打扰模式
channel.setBypassDnd(true);
//设置震动模式
channel.setVibrationPattern(new long[]{100, 100, 200});
//是否会有灯光
channel.shouldShowLights();
getManager().createNotificationChannel(channel);
} private NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
} /**
* 发送通知
*/
public void sendNotification(String title, String content) {
NotificationCompat.Builder builder = getNotification(title, content);
getManager().notify(1, builder.build());
} private NotificationCompat.Builder getNotification(String title, String content) {
NotificationCompat.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
} else {
builder = new NotificationCompat.Builder(getApplicationContext());
builder.setPriority(PRIORITY_DEFAULT);
}
//标题
builder.setContentTitle(title);
//文本内容
builder.setContentText(content);
//小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置点击信息后自动清除通知
builder.setAutoCancel(true);
Intent clickIntent = new Intent(); //点击通知之后要发送的广播
int id = (int) (System.currentTimeMillis() / 1000);
clickIntent.addCategory(context.getPackageName());
clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED);
clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, EXTRA_EXTRA);
PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); return builder;
} /**
* 发送通知
*/
public void sendNotification(int notifyId, String title, String content) {
NotificationCompat.Builder builder = getNotification(title, content);
getManager().notify(notifyId, builder.build());
} }  

Android8.0通知的更多相关文章

  1. Android深入四大组件(五)Android8.0 根Activity启动过程(后篇)

    前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...

  2. Android8.0适配那点事(二)

    小伙伴们,咱们今天咱继续对Android8.0的适配进行分解,今天将针对启动页,版本适配和系统限制等进行“啃食” 猛戳这里查看Android8.0适配那点事(一): 1.启动页适配 近日,我无意中发现 ...

  3. Android:Mstar Android8.0平台音量控制流程

    一.Speaker 音量.静音流程分析 java层音量设置首先调用到的是AudioManager.java中的方法,在这里有两种方法可以设置音量 setStreamVolume 和 adjustStr ...

  4. Android8.0 后台服务保活的一种思路

    原文地址:Android8.0 后台服务保活的一种思路 | Stars-One的杂货小窝 项目中有个MQ服务,需要一直连着,接收到消息会发送语音,且手机要在锁屏也要实现此功能 目前是使用广播机制实现, ...

  5. Android8.0运行时权限策略变化和适配方案

    版权声明:转载必须注明本文转自严振杰的博客:http://blog.yanzhenjie.comAndroid8.0也就是Android O即将要发布了,有很多新特性,目前我们可以通过AndroidS ...

  6. Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息

    由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...

  7. Android深入四大组件(四)Android8.0 根Activity启动过程(前篇)

    前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...

  8. cocos2d-x android8.0 视频层遮挡问题

    cocos里默认情况下视频层是在cocos 层的上面,如果希望把视频层放在cocos的下面的话, android8.0以下,把Cocos2dxVideoHelper.java里的 videoView. ...

  9. 解决Android Screen Monitor在android8.0及以上系统报错:"E/Screenshot: Unsupported protocol: 2"

    1.打开命令窗口,切换到 asm.jar 所在目录,执行 java -jar asm.jar,正常情况下打开后连接上设备会显示出画面 2.但是在android8.0以上系统该asm.jar包就无法正常 ...

随机推荐

  1. 创建第一个core项目(netCore学习笔记1)

    1.安装 core和netFramework其实是相对独立的,但是core的IDE是在vs2017才开始支持,而vs2017的安装环境必须搭配.net4.6,所以: Step1:安装.net4.6 S ...

  2. csrf攻击与防范

    CSRF(Cross Site Request Forgeries)跨网站请求伪造,也叫XSRF,通过伪装来自受信任用户的请求来攻击利用受信任网站. 与对比 xss:本网站运行了来自其它网站的脚本 c ...

  3. Oracle-05:伪表dual

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 此乃这张表里的全部字段信息,此表无法做修改操作 为什么有伪表这个概念?????????? 因为sql92的一个 ...

  4. MATCH_PARENT和FILL_PARENT之间的区别?

    很多人表示对于很多工程中的MATCH_PARENT出现在layout中感到不明白,过去只有FILL_PARENT和WRAP_CONTENT那么 match_parent到底是什么类型呢? 其实从And ...

  5. 深夜学算法之SkipList:让链表飞

    1. 前言 上次写Python操作LevelDB时提到过,有机会要实现下SkipList.摘录下wiki介绍: 跳跃列表是一种随机化数据结构,基于并联的链表,其效率可比拟二叉查找树. 我们知道对于有序 ...

  6. HoloLens开发手记-实现3D应用启动器

    一直有人问我,第三方应用在HoloLens上可以实现3D启动图标吗?就像微软官方出的应用那样.在不久之前,这个问题的答案还是No. 但是随着最新的Windows build 1803版本的发布,现在我 ...

  7. FPGA学习笔记(二)——FPGA学习路线及开发流程

    ###### [该随笔部分内容转载自小梅哥]       ######### 一.FPGA学习路线 工具使用 -> 语法学习 -> 逻辑设计 -> IP使用 ->接口设计 -& ...

  8. 玩转web之ligerui(一)---ligerGrid重新指定url

    请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. 在特定情况下,我们需要重新指定ligerGrid的url来获取不同的数据,在这里我说一下我用的方法: 首先先定义一个全局变量,然后定义liger ...

  9. selenium的一些使用方法

    新建实例driver = webdriver.Chrome()1.通过标签属性Id查找元素方法:find_element_by_id(element_id)实例:driver.find_element ...

  10. Eclipse中Maven Install时发生错误

    问题描述 要把一个本地包保存进本地maven库中, 所以对该project执行了run as => Maven Install, 结果报下面的错误. 解决办法 1. 通过命令窗口手动创建这两个文 ...