//开启通知权限

private void checkNotifySetting() {

boolean isOpened = NotificationUtils.isNotificationEnabled(this);

if (isOpened) {

LogUtil.d(LogUtil.L, “通知权限已经被打开” +

“\n手机型号:” + android.os.Build.MODEL +

“\nSDK版本:” + android.os.Build.VERSION.SDK +

“\n系统版本:” + android.os.Build.VERSION.RELEASE +

“\n软件包名:” + getPackageName());

} else {

LogUtil.d(LogUtil.L, “还没有开启通知权限,点击去开启”);

SaveUtil.saveNotiyPression(false);

openNotifyDialog();

}

}

private void openNotifyDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(

MainActivity2.this);

LayoutInflater inflater = LayoutInflater.from(

MainActivity2.this);

View v = inflater.inflate(R.layout.layout_notif_dialog, null);

TextView tvMessage = v.findViewById(R.id.tv_message);

tvMessage.setText(R.string.label_notiy_message);

Button btnCancel = v.findViewById(R.id.btn_cancel);

Button btnSubmit = v.findViewById(R.id.btn_submit);

btnCancel.setText(R.string.label_notiy_cancel);

btnSubmit.setText(R.string.label_notiy_submit);

final AlertDialog dialog = builder.create();

dialog.show();

dialog.getWindow().setContentView(v);

dialog.getWindow().setGravity(Gravity.CENTER);

btnCancel.setOnClickListener(new View.OnClickListener() {

@Override

public void onCl

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ick(View v) {

dialog.dismiss();

}

});

btnSubmit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

dialog.dismiss();

NotificationUtils.openPush(MainActivity2.this);

}

});

}

NotificationUtils 通知类

/**

  • desc:通知生成类

*/

public class NotificationUtils extends ContextWrapper {

/**

  • 通知管理对象

*/

private NotificationManager notificationManager;

/**

  • channel的ID

*/

public static final String id = “channel_id”;

/**

  • channel的名称

*/

public static final String name = “channel_name”;

/**

  • notification id

*/

public static final int notification_id = 1;

//最多显示3条通知

public int NOTIFICATION_SHOW_SHOW_AT_MOST = 3;

/**

  • 通知生成类的构造方法

*/

public NotificationUtils(Context context) {

super(context);

initWindowManager(context);

}

private WindowManager.LayoutParams mWindowParams;

private WindowManager mWindowManager;

private void initWindowManager(Context context) {

mWindowParams = new WindowManager.LayoutParams();

mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

if (Build.VERSION.SDK_INT >= 26) {//8.0新特性

mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

} else {

mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

}

}

/**

  • 模拟发送一个普通通知

  • @param iconRes

  • @param title

  • @param content

  • @param pendingIntent

*/

public void sendNotification(int iconRes, String title, String content, PendingIntent pendingIntent) {

// int num = SaveUtil.getNotiyNum();

// num++;

// SaveUtil.saveNotiyNum(num);

// //通知条数<10

// if (SaveUtil.getNotiyNum() > NOTIFICATION_SHOW_SHOW_AT_MOST) {

// SaveUtil.saveNotiyNum(1);

// }

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

//26及以上

createNotificationChannel();

Notification notification = getChannelNotification(iconRes, title, content, pendingIntent).build();

notificationManager.notify(notification_id, notification);

// notificationManager.notify(SaveUtil.getNotiyNum(), notification);

} else {

getNotificationManager();

Notification notification = getNotification(iconRes, title, content, pendingIntent).build();

notificationManager.notify(notification_id, notification);

// notificationManager.notify(SaveUtil.getNotiyNum(), notification);

}

}

/**

  • 创建NotificationChannel

*/

public void createNotificationChannel() {

NotificationChannel notificationChannel = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

// notificationChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);

// notificationChannel.canBypassDnd();//可否绕过请勿打扰模式

// notificationChannel.canShowBadge();//桌面lanchener显示角标

// notificationChannel.enableLights(true);//闪光

// notificationChannel.shouldShowLights();//闪光

// notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//锁屏显示通知

// notificationChannel.enableVibration(true);//是否允许震动

// notificationChannel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式

// notificationChannel.getAudioAttributes();//获取系统响铃配置

// notificationChannel.getGroup();//获取消息渠道组

// notificationChannel.setBypassDnd(true);

// notificationChannel.setDescription(“description”);

// notificationChannel.setLightColor(Color.GREEN);//制定闪灯是灯光颜色

// notificationChannel.setShowBadge(true);

// getNotificationManager().createNotificationChannel(notificationChannel);

//第一个参数:channel_id

//第二个参数:channel_name

//第三个参数:设置通知重要性级别

//注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;

//范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)

NotificationChannel channel = new NotificationChannel(id, name,

NotificationManager.IMPORTANCE_DEFAULT);

// channel.canBypassDnd();//是否绕过请勿打扰模式

channel.enableLights(true);//闪光灯

channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知

channel.setLightColor(Color.RED);//闪关灯的灯光颜色

channel.canShowBadge();//桌面launcher的消息角标

channel.enableVibration(true);//是否允许震动

channel.getAudioAttributes();//获取系统通知响铃声音的配置

channel.getGroup();//获取通知取到组

channel.setBypassDnd(true);//设置可绕过 请勿打扰模式

channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式

channel.shouldShowLights();//是否会有灯光

getNotificationManager().createNotificationChannel(channel);

}

}

/**

  • 获取通知管理者对象

  • @return

*/

public NotificationManager getNotificationManager() {

if (notificationManager == null) {

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

}

return notificationManager;

}

/**

  • 对应Android8.0生成notification的方法,通过此方法获取notification

*/

public Notification.Builder getChannelNotification(int iconRes, String title, String content, PendingIntent pendingIntent) {

Notification.Builder builder = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

builder = new Notification.Builder(getApplicationContext(), id);

builder.setSmallIcon(iconRes);

builder.setAutoCancel(true);

builder.setChannelId(id);

builder.setWhen(System.currentTimeMillis());

builder.setContentTitle(title);

//设置显示通知时间

builder.setShowWhen(true);

builder.setContentText(content);

builder.setNumber(3);

builder.setOnlyAlertOnce(false);

//悬停通知

builder.setTicker(content);

builder.setDefaults(~0);

builder.setPriority(Notification.PRIORITY_DEFAULT);

// builder.setVisibility(Notification.VISIBILITY_PUBLIC);

// builder.setFullScreenIntent(pendingIntent, true);

builder.setContentIntent(pendingIntent);

}

return builder;

}

private int priority = Notification.PRIORITY_DEFAULT;

/**

  • 对应Android8.0以下的notification对象

*/

public NotificationCompat.Builder getNotification(int iconRes, String title, String content, PendingIntent pendingIntent) {

// NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), id);

builder.setPriority(NotificationCompat.PRIORITY_MAX);

builder.setSmallIcon(iconRes);

builder.setAutoCancel(true);

builder.setWhen(System.currentTimeMillis());

builder.setContentTitle(title); //设置标题

builder.setContentText(content);

builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置振动声音等,需要振动权限

builder.setContentIntent(pendingIntent); //自定义打开的界面

//悬停通知

builder.setTicker(title);

builder.setDefaults(~0);

builder.setPriority(Notification.PRIORITY_HIGH);

// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本>=21才能设置悬挂式通知栏

// builder.setCategory(String.valueOf(Notification.FLAG_ONGOING_EVENT))

// .setVisibility(Notification.VISIBILITY_PUBLIC);

// }

// builder.setVisibility(Notification.VISIBILITY_PUBLIC);

// builder.setFullScreenIntent(pendingIntent, true);

//点击自动删除通知

builder.setAutoCancel(true);

return builder;

}

// public static boolean isNotificationEnabled(Context context,String channelId) {

// return NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled();

// }

// public static boolean isNotificationEnabled(Context context,String channelId) {

// NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);

// NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// boolean returnValue = managerCompat.areNotificationsEnabled();

// if(manager == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.O){

// return returnValue;

// }

// NotificationChannel channel = manager.getNotificationChannel(channelId);

// if(channel == null){

// channel = new NotificationChannel(channelId,“我的推送类别”,NotificationManager.IMPORTANCE_HIGH);

// manager.createNotificationChannel(channel);

//

// //下面的获取操作必需,创建的channel和获取到的channel的IMPORTANCE可能不一样,OPPO默认IMPORTANCE_NONE。

// channel = manager.getNotificationChannel(channelId);

// }

// return returnValue && channel.getImportance() != NotificationManager.IMPORTANCE_NONE;

// }

areNotificationsEnabled方法的有效性官方只最低支持到API 19,

/// 低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。

//查阅官方文档可知 NotificationManagerCompat 在 android.support.v4.app包中,

// 是API 22.1.0 中加入的。而 areNotificationsEnabled()则是在 API 24.1.0之后加入的

//areNotificationsEnabled 只对 API 19 及以上版本有效,低于API 19 会一直返回true

public static boolean isNotificationEnabled(Context context) {

return NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled();

}

public static void openPush(Activity activity) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

//这种方案适用于 API 26, 即8.0(含8.0)以上可以用

Android 关于推送通知还需要一些其他的设置问题,最新高频Android笔试题分享相关推荐

  1. android 华为推送通知中心,华为推送通知栏消息功能及 intent 说明文档

    华为推送分为透传推送和通知栏推送两种方式. 两种方式的区别如下: 透传推送:华为推送服务会将原始 json 数据发给目标客户端,客户端内嵌的融云 SDK 接受到该 json 数据后,会进行解析并在通知 ...

  2. 如何使用SAP云平台的Notification服务给Android应用推送通知消息

    登录SAP Cloud Platform Mobile Services management cockpit: 在Mobile Application列表里,选择之前创建好的Wiz App: 打开U ...

  3. nb-iot_IoT项目:Arduino使用Parse.com的Temboo向Android发送推送通知

    nb-iot 这篇文章介绍了如何创建一个IoT项目,该项目使用Arduino通过Temboo和Parse.com将推送消息发送到Android智能手机. 例如,我们将构建一个基于Arduino和And ...

  4. Android推送通知指南(转)

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  5. Android 推送通知指南(转载)

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  6. Android推送通知指南

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  7. Android 服务器推送技术

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  8. 总结的太牛了,android实时推送!

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  9. Android 实现推送功能

    近几天正研究Android推送的事,看到这篇文章觉得总结得真好,好东西一定要转过来.哈哈. 原文地址:http://blog.csdn.net/joshua_yu/article/details/65 ...

  10. Android消息推送机制

    1.推送方式基础知识: 当我们开发需要和服务器交互的应用程序时,基本上都需要获取服务器端的数据,比如<地震应急通>就需要及时获取服务器上最新的地震信息.要获取服务器 上不定时更新的信息一般 ...

最新文章

  1. 导入已有工程相关问题解决实录
  2. 4 Git 分支 - 分支开发工作流
  3. 字符串、对象、数组操作方法、json方法
  4. Axios的Vue插件(添加全局请求/响应拦截器)
  5. 互联网晚报 | 04月05日 星期二 |​ ​​​考研调剂系统明日开通;微软Edge浏览器宣布10天后退出历史舞台...
  6. 《Web异步与实时交互——iframe AJAX WebSocket开发实战》—— 1.4 内容安排
  7. Netty工作笔记0022---NIO快速入门--编写客户端
  8. comsenzexp mysql密码_ComsenzEXP X3
  9. 一阶rc电路时间常数_关于RC延时电路的延时时间计算
  10. 计算机办公应用教案,计算机应用基础与办公自动化教案
  11. ASIO音频驱动开发指南 2.0
  12. 使用参数非参数和机器学习方法分析印度降雨变化,能给我国带来什么警示?
  13. Java学习笔记(十)——开发个小项目(GoBang2.0)
  14. 华硕笔记本(GTX 1060显卡)安装Ubuntu16.04+Nvidia显卡驱动+Cuda8.0+cudnn6.0+ROS+Opencv3.2+Caffe+Tensorflow
  15. 陈强教授《机器学习及R应用》课程 第四章作业
  16. eas 税率修改_5月1日开始,金蝶ERP系统税率调整方法与步骤
  17. 398高校毕业设计选题
  18. Linux多网卡绑定(bond)及网络组(team)
  19. 保存二维码图片到手机相册
  20. js动态添加修改删除元素

热门文章

  1. 计算机主机号截图,电脑如何截图?截图三种方法推荐
  2. 毕业写论文不要傻傻的到中国知网CNKI充值了,分享几个常用的写论文必备的网站!
  3. Mac 好用的 Android 模拟器整理(玩游戏、装应用、支持咸鱼、拼多多...)
  4. 二分算法:数的三次方根
  5. 如何把照片转成pdf文件,支持合并转换
  6. 平时工作中如何体现个人技术深度?
  7. xdb 服务_oracle XDB的问题,端口、http服务
  8. UPDATE更新数据库数据详解
  9. 尺度、空间异质性、干扰、景观多样性、景观连接度,对其概念的理解
  10. php怎么解析josn数据,用PHP解析JSON数据