Android 关于推送通知还需要一些其他的设置问题

//最多显示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)以上可以用

Intent intent = new Intent();

intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);

intent.putExtra(Settings.EXTRA_APP_PACKAGE, activity.getPackageName());

intent.putExtra(Settings.EXTRA_CHANNEL_ID, activity.getApplicationInfo().uid);

activity.startActivity(intent);

} else {

PermissionUtil.toPermissionSetting(activity);

}

}

2.后台运行设置

1.电池优化设置

2.后台运行设置

3.显示在上层设置

private EasyPopup mStartManagerPop;

private void showStartManagerPop() {

// 将对话框的大小按屏幕大小的百分比设置

WindowManager windowManager = getWindowManager();

Display display = windowManager.getDefaultDisplay();

mStartManagerPop = EasyPopup.create()

.setContentView(this, R.layout.layout_startmanager)

.setOnViewListener(new EasyPopup.OnViewListener() {

@Override

public void initViews(View view, final EasyPopup easyPopup) {

ImageView mIvStartBg = view.findViewById(R.id.iv_start_bg);

ImageView mIvCircle1 = view.findViewById(R.id.iv_circle1);

ImageView mIvCircle2 = view.findViewById(R.id.iv_circle2);

mIvStartBg.setImageResource(BuildConfig.FLAVOR.equals(Common.Constance.Smartlock) ?

R.drawable.ic_start_manager : R.drawable.ug_ic_start_manager);

mIvCircle1.setImageResource(BuildConfig.FLAVOR.equals(Common.Constance.Smartlock) ?

R.drawable.ic_start_circle : R.drawable.ug_ic_start_circle);

mIvCircle2.setImageResource(BuildConfig.FLAVOR.equals(Common.Constance.Smartlock) ?

R.drawable.ic_start_circle : R.drawable.ug_ic_start_circle);

TextView txvContent=view.findViewById(R.id.txv_content);

txvContent.setText(BuildConfig.FLAVOR.equals(Common.Constance.Smartlock) ?

“允许 “保仕盾” 始终在后台运行,可能会缩短电池的续航时间。” : “允许 “优果智能” 始终在后台运行,可能会缩短电池的续航时间。”);

view.findViewById(R.id.tv_ok).setOnClickListener(v -> {

if (isIgnoringBatteryOptimizations()) {

BackStageKeepAliveUtil.getDeviceSetting(BackstageSetActivity.this);

} else {

requestIgnoreBatteryOptimizations();

}

mStartManagerPop.dismiss();

});

view.findViewById(R.id.tv_cancel).setOnClickListener(v -> mStartManagerPop.dismiss());

}

})

.setWidth((int) (display.getWidth() * 0.85))

.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)

.setAnimationStyle(R.style.pop_privacy)

.setBackgroundDimEnable(true)

.setFocusAndOutsideEnable(true)

.setOnDismissListener(() -> {

mStartManagerPop.dismiss();

})
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

算法知识点繁多,企业考察的题目千变万化,面对越来越近的“金九银十”,我给大家准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。

这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。

部分资料展示:


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

(img-d2dxLqJE-1713734393157)]

总结

算法知识点繁多,企业考察的题目千变万化,面对越来越近的“金九银十”,我给大家准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。

这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。

部分资料展示:

[外链图片转存中…(img-ksI3kWiE-1713734393158)]
[外链图片转存中…(img-0KLxEQPi-1713734393159)]
[外链图片转存中…(img-eqK1lOk6-1713734393160)]
[外链图片转存中…(img-CjjUIjuO-1713734393161)]

有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 24
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 离线推送中,可以通过设置 Notification 样式来自定义通知外观。以下是一些常见的 Notification 样式及设置方法: 1. BigTextStyle:展示一个大文本区域,可以显示更多的文字内容。 ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setContentTitle("Title") .setContentText("Content") .setStyle(new NotificationCompat.BigTextStyle() .bigText("Big Text")) .setSmallIcon(R.drawable.ic_notification); ``` 2. InboxStyle:展示一个收件箱,可以显示多个文本条目。 ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setContentTitle("Title") .setContentText("Content") .setStyle(new NotificationCompat.InboxStyle() .addLine("Line 1") .addLine("Line 2") .addLine("Line 3")) .setSmallIcon(R.drawable.ic_notification); ``` 3. BigPictureStyle:展示一张大图,可以显示更多的图片内容。 ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setContentTitle("Title") .setContentText("Content") .setStyle(new NotificationCompat.BigPictureStyle() .bigPicture(bitmap) .bigLargeIcon(null)) .setSmallIcon(R.drawable.ic_notification); ``` 4. MessagingStyle:展示聊天消息,可以显示多个消息条目。 ```java NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setContentTitle("Title") .setContentText("Content") .setStyle(new NotificationCompat.MessagingStyle("Me") .addMessage("Message 1", timestamp1, null) .addMessage("Message 2", timestamp2, "Sender 2")) .setSmallIcon(R.drawable.ic_notification); ``` 可以在应用程序中根据需要设置上述样式,同时可以设置通知的标题、内容、图标、声音等属性。请注意,自定义通知样式可能会因 Android 系统版本或厂商定制的不同而有所不同,需要进行测试和适配。同时,还需要AndroidManifest.xml 文件中配置相应的权限和服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值