// 引入相关 Android 类
var NotificationManager = android.app.NotificationManager;
var Context = android.content.Context;
var PackageManager = android.content.pm.PackageManager;
var Icon = android.graphics.drawable.Icon;
var PendingIntent = android.app.PendingIntent;
var Intent = android.content.Intent;
var ComponentName = android.content.ComponentName;
// 定义发送通知的函数
function sendNotification(packageName, title, content, iconResName, channelId, intentUri) {
try {
var context = android.app.ActivityThread.currentApplication(); // 获取当前应用的上下文
// 获取目标应用的上下文
var appContext = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
// 获取目标应用的 PackageManager 和资源
var packageManager = context.getPackageManager();
var resources = appContext.getResources();
// 获取目标应用的默认图标
var notificationIconResourceId = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).icon; // 默认图标
// 尝试获取专门的通知图标
var notificationIconResId = resources.getIdentifier(iconResName, "drawable", packageName);
if (notificationIconResId !== 0) {
notificationIconResourceId = notificationIconResId; // 更新为专门的通知图标
console.log("找到了通知图标:" + iconResName);
} else {
console.log("未找到专门的通知图标,使用默认图标");
}
// 获取目标应用的 NotificationManager
var notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE);
// 获取目标应用的所有通知渠道
var channels = notificationManager.getNotificationChannels();
var selectedChannel = null;
// 尝试查找指定的渠道
if (channelId) {
for (var i = 0; i < channels.size(); i++) {
var tempChannel = channels.get(i);
if (tempChannel.getId() == channelId) {
selectedChannel = tempChannel;
break;
}
}
}
// 如果找不到指定的渠道,随机选择一个渠道
if (!selectedChannel && channels.size() > 0) {
var randomIndex = Math.floor(Math.random() * channels.size()); // 随机选择一个渠道
selectedChannel = channels.get(randomIndex);
console.log("没有找到指定的通知渠道,随机选择了渠道:" + selectedChannel.getId());
} else if (selectedChannel) {
console.log("找到了指定的通知渠道:" + selectedChannel.getId());
} else {
console.log("目标应用没有任何通知渠道。");
}
// 如果找到了渠道,发送通知
if (selectedChannel) {
// 解析 Intent URI 字符串
var clickIntent = Intent.parseUri(intentUri, 0);
// 确保 Intent 具有正确的 flags 和参数
clickIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); // 保证新的任务栈
var pendingIntent = PendingIntent.getActivity(
appContext,
0,
clickIntent,
PendingIntent.FLAG_IMMUTABLE // 设置 PendingIntent 为不可变
);
// 创建通知对象
var builder = new android.app.Notification$Builder(appContext, selectedChannel.getId())
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(notificationIconResourceId) // 使用目标应用的图标作为 smallIcon
.setContentIntent(pendingIntent) // 设置点击通知后的操作
.setAutoCancel(true); // 点击后自动取消通知
// 生成通知
var notification = builder.build();
// 发送通知
notificationManager.notify(1, notification); // 使用唯一的通知 ID 发送通知
console.log("通知已发送,ID:1");
} else {
console.log("没有找到合适的渠道,通知未发送。");
}
} catch (e) {
console.log("发送通知时发生错误: " + e.message);
}
}
// 使用示例
sendNotification(
"com.tencent.mm", // 目标应用包名
"这是通知标题", // 通知标题
"这是通知的内容", // 通知内容
"e0", // 图标资源名称
"message_channel_new_id", // 通知渠道 ID
"intent" // 使用你的 intent 字符串
);以应用身份发送通知