牙叔教程 简单易懂
效果
完整源码
importClass(android.content.ComponentName); importClass(android.app.PendingIntent); importClass(android.content.Context); importClass(android.graphics.Bitmap); importClass(android.graphics.drawable.Icon); importClass(android.app.NotificationChannel); importClass(android.app.NotificationManager); importClass(android.app.Notification); /* -------------------------------------------------------------------------- */ let icon = getIcon(); log(icon.small); events.on("exit", function () { icon.large.recycle(); }); notice("牙叔教程 老棒了"); /* -------------------------------------------------------------------------- */ function notice(msg) { var manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE); var channel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH); channel.setShowBadge(false); // 显示徽章, 桌面app图标右上角有小红点 manager.createNotificationChannel(channel); notification = new Notification.Builder(context, "id") .setContentTitle("公众号: 牙叔教程") .setSubText("牙叔教程 简单易懂") .setContentText(msg) .setSmallIcon(icon.small) .setLargeIcon(icon.large) .setTicker("this is ticker") .setAutoCancel(true) .setContentIntent( PendingIntent.getActivity( context, 0, new Intent().setComponent( new ComponentName(context.getPackageName(), "org.autojs.autojs.ui.main.MainActivity") ), PendingIntent.FLAG_UPDATE_CURRENT ) ) .build(); manager.notify(0, notification); log("1"); } function getIcon() { let bitmap = createBitmap(); return { small: Icon.createWithBitmap(bitmap), large: bitmap, }; } function createBitmap() { // 24dp let size = 36; let width = dp2px(size); let paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); let bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888); let colorList = ["#dd7694", "#c3d94e", "#535164", "#779649"]; let canvas = new Canvas(bitmap); paint.setColor(colors.parseColor(colorList[0])); canvas.drawRect(0, 0, width / 2, width / 2, paint); paint.setColor(colors.parseColor(colorList[1])); canvas.drawRect(width / 2, 0, width, width / 2, paint); paint.setColor(colors.parseColor(colorList[2])); canvas.drawRect(0, width / 2, (width / 2) * 1, width, paint); paint.setColor(colors.parseColor(colorList[3])); canvas.drawRect(width / 2, width / 2, width, width, paint); return bitmap; } function dp2px(dp) { const scale = context.getResources().getDisplayMetrics().density; return Math.floor(dp * scale + 0.5); }
备注
通知栏图标一共两个
左侧是小的, 右侧是大的
左侧通过setSmallIcon设置
经测试,
在华为手机上, 左侧小图标显示正常;
在小米手机上, 左侧小图标一直显示为app的logo,
也就是说在小米手机上无法修改.
打包时, 修改app的logo, 这样通知的小图标就改了
环境
手机: Mi 11 Pro
Android版本: 12
Autojs版本: 9.1.8
相关教程
https://www.yuque.com/go/doc/52685290
https://www.yuque.com/go/doc/65146895
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途