Android8.0通知
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通知的更多相关文章
- Android深入四大组件(五)Android8.0 根Activity启动过程(后篇)
前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...
- Android8.0适配那点事(二)
小伙伴们,咱们今天咱继续对Android8.0的适配进行分解,今天将针对启动页,版本适配和系统限制等进行“啃食” 猛戳这里查看Android8.0适配那点事(一): 1.启动页适配 近日,我无意中发现 ...
- Android:Mstar Android8.0平台音量控制流程
一.Speaker 音量.静音流程分析 java层音量设置首先调用到的是AudioManager.java中的方法,在这里有两种方法可以设置音量 setStreamVolume 和 adjustStr ...
- Android8.0 后台服务保活的一种思路
原文地址:Android8.0 后台服务保活的一种思路 | Stars-One的杂货小窝 项目中有个MQ服务,需要一直连着,接收到消息会发送语音,且手机要在锁屏也要实现此功能 目前是使用广播机制实现, ...
- Android8.0运行时权限策略变化和适配方案
版权声明:转载必须注明本文转自严振杰的博客:http://blog.yanzhenjie.comAndroid8.0也就是Android O即将要发布了,有很多新特性,目前我们可以通过AndroidS ...
- Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息
由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...
- Android深入四大组件(四)Android8.0 根Activity启动过程(前篇)
前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...
- cocos2d-x android8.0 视频层遮挡问题
cocos里默认情况下视频层是在cocos 层的上面,如果希望把视频层放在cocos的下面的话, android8.0以下,把Cocos2dxVideoHelper.java里的 videoView. ...
- 解决Android Screen Monitor在android8.0及以上系统报错:"E/Screenshot: Unsupported protocol: 2"
1.打开命令窗口,切换到 asm.jar 所在目录,执行 java -jar asm.jar,正常情况下打开后连接上设备会显示出画面 2.但是在android8.0以上系统该asm.jar包就无法正常 ...
随机推荐
- Pat1067:Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- SSM-SpringMVC-13:SpringMVC中XmlViewResolver视图解析器
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 还记得上篇博客提出来的问题吗? BeanNameViewResolver视图解析器每使用一道视图,就得手工配 ...
- 阅读GIC-500 Technical Reference Manual笔记
GIC-500是ARM GICv3的一个实现,它只支持ARMv8核和实现了GIC Stream协议的GIC CPU Interface,比如Cortex-A53. 关于GIC有四份相关文档:<C ...
- ORALCE删除临时表空间的方法---解决ORA01033: oralce initialization or shutdown in progress方案
当一台主机上oralce 临时表空间太多,而又用不到这些临时表空间的时候, TABLESPACE 会占用大量的存储空间.本文介绍一种删除ORACLE 临时表空间的方法. 一 启动任务管理器.在任 ...
- 搭建微信小程序服务
准备域名和证书 任务时间:20min ~ 40min 小程序后台服务需要通过 HTTPS 访问,在实验开始之前,我们要准备域名和 SSL 证书. 域名注册 如果您还没有域名,可以在腾讯云上选购,过程可 ...
- Hibernate验证器
第 4 章 Hibernate验证器 http://hibernate.org/validator/documentation/getting-started/#applying-constrain ...
- range与enumerate的区别
在迭代中enumerate比range更能灵活,一般情况下尽量用erumerate,下面举例说明: 先来看range的使用: city_list = ['beijing', 'shanghai', ' ...
- dom操作相关,byebye T T
o = { name: 'aa', price: 11, } function add(items) { var bodys = document.getElementsByTagName('tbod ...
- disk.go
package disk import "syscall" //空间使用结构体 type DiskStatus struct { Size uint64 Used ...
- BZOJ_4765_普通计算姬_分块+dfs序+树状数组
BZOJ_4765_普通计算姬_分块 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能 ...