autojs发送通知修改图标

简介: 牙叔教程 简单易懂

牙叔教程 简单易懂


效果

完整源码

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文档, 最后才是群里问问
--- 牙叔教程


声明


部分内容来自网络
本教程仅用于学习, 禁止用于其他用途



相关文章
|
API
AutoJs4.1.0开发心得
AutoJs4.1.0开发心得
253 0
|
人工智能 前端开发 Java
autojs非常见函数1
牙叔教程 简单易懂
2444 0
|
Android开发
autojs之保活
autojs打包的app经常在后台被杀,请做到以下几点来保活: autojs版本号
2513 0
autojs之保活
|
Android开发
autojs修改悬浮窗按钮点击事件
牙叔教程 简单易懂
1635 0
|
SpringCloudAlibaba
SpringCloudAlibaba踩坑日记(二)Relying upon circular references is discouraged and they are prohibited by
SpringCloudAlibaba踩坑日记(二)Relying upon circular references is discouraged and they are prohibited by
4230 0
SpringCloudAlibaba踩坑日记(二)Relying upon circular references is discouraged and they are prohibited by
|
JavaScript
Vue3抽屉(Drawer)
这是一个基于Vue的抽屉组件(Drawer),提供了丰富的自定义选项,如宽度、高度、标题、关闭按钮等,并支持顶部、右侧、底部和左侧四种方向。
578 0
Vue3抽屉(Drawer)
|
Shell 网络安全 开发工具
Gerrit✨Gerrit服务器简介 与 配置SSH keys
Gerrit✨Gerrit服务器简介 与 配置SSH keys
|
JSON 安全 API
技术经验解读:使用Refit框架访问REST接口
技术经验解读:使用Refit框架访问REST接口
291 0
|
数据安全/隐私保护 Windows
Windows使用远程桌面连接树莓派
Windows使用远程桌面连接树莓派
1353 0
Windows使用远程桌面连接树莓派