目录
##网上流行的代码
"ui";
ui.layout(
<vertical w="*" h="*" gravity="center" >
<button id="notify" w="auto" h="auto" text="发送通知" textSize="22sp" padding="12dp" />
<button id="cancel" w="auto" h="auto" text="去除通知" textSize="22sp" padding="12dp" />
</vertical>
);
ui.notify.on("click", () => {
var manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE);
var notification;
if (device.sdkInt >= 26) {
var channel = new android.app.NotificationChannel("channel_id", "channel_name", android.app.NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setLightColor(0xff0000);
channel.setShowBadge(false);
manager.createNotificationChannel(channel);
notification = new android.app.Notification.Builder(context, "channel_id")
.setContentTitle("通知栏标题"+new date())
.setContentText("这是消息的内容")
.setWhen(new Date().getTime())
.setSmallIcon(org.autojs.autojs.R.drawable.autojs_material)
.setTicker("这是状态栏显示的内容")
.build();
} else {
notification = new android.app.Notification.Builder(context)
.setContentTitle("通知栏标题")
.setContentText("这是消息的内容")
.setWhen(new Date().getTime())
.setSmallIcon(org.autojs.autojs.R.drawable.autojs_material)
.setTicker("这是状态栏显示的内容")
.build();
}
manager.notify(1, notification);
});
ui.cancel.on("click", () => {
var manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE);
manager.cancelAll();
// manager.cancel(1);
});
存在问题
- setWhen 无效
不会显示时间,因为默认是关闭的,需要设置 .setShowWhen(true)
For apps targeting Build.VERSION_CODES.N and above, this defaults to false. For earlier apps, the default is true
翻译:
对于定位Build.VERSION_CODES.N及以上的应用程序,这默认为false。对于较早的应用程序,默认值为true.
这里的Build.VERSION_CODES.N是安卓7,也就是安卓7以上默认不显示,需要设置为true
-
通知不能点击,无法回到应用界面
因为没有设置ContentIntent,所以只是一个显示栏。
-
图标是默认的,嗯。。。。
我的代码
发送通知
notification.js
importClass("android.app.NotificationManager");
importClass("android.app.Notification");
importClass("android.app.PendingIntent");
importClass("android.content.res.Resources");
importClass("android.app.PendingIntent");
module.exports = {
send(title,content,ticker){
let channel_id = 'channel_id';
var manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE);
var notification;
var icon = context.getResources().getIdentifier("ic_3d_rotation_black_48dp", "drawable", context.getPackageName());
let contentIntent = PendingIntent.getActivity(context, 0, app.intent({
packageName: currentPackage(),
className: currentActivity()
}), 0);
if (device.sdkInt >= 26) {
var channel = new android.app.NotificationChannel(channel_id, title?title:"通知测试", android.app.NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setLightColor(0xff0000);
channel.setShowBadge(false);
manager.createNotificationChannel(channel);
notification = new android.app.Notification.Builder(context, channel_id)
.setContentTitle(title || '')
.setContentText(content || '')
.setWhen((new Date()).getTime())
.setSmallIcon(icon)
.setTicker(ticker || '')
.setOngoing(true)
.setShowWhen(true)
.setContentIntent(contentIntent);
} else {
notification = new android.app.Notification.Builder(context)
.setContentTitle(title || '')
.setContentText(content || '')
.setWhen((new Date()).getTime())
.setSmallIcon(icon)
.setTicker(ticker || '');
}
manager.notify(1, notification.build());
return {
'manager':manager,
'notification_builder':notification
};
},
cancel(){
var manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE);
manager.cancelAll();
},
}
发送通知
let nn = require('./notification')
nn.send('通知','通知内容','')
更新通知
let nn = require('./notification')
var notification_retrun_obj= {}
//发送通知
notification_retrun_obj= nn.send('正在运行中','此为前台保活,请勿手动移除该通知','')
// 更新通知
let n = notification_retrun_obj.notification_builder.setWhen((new Date()).getTime()).setContentText('此为前台保活,请勿手动移除该通知 '+(new Date()).getTime()).build()
notification_retrun_obj.manager.notify(1,n)
是如何更新通知的?
- 使用原来的Notification.Builder
let manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE);
let builder = new android.app.Notification.Builder(context, channel_id)
- 更新内容
builder.setContentText('新内容')
- 再次build
let n = builder.build()
- 使用原来的id发送
manager.notify(1,n)
参考
Android Developers>Docs>Reference>NotificationCompat.Builder
android-更新通知文本,而不是整个通知