const androidNotify = (content, payload) => {
// #ifdef APP-PLUS
console.log('准备通知');
console.log(plus.os.name);
//Android平台下才使用此推送
if (plus.os.name != 'Android') {
return false;
}
let firstVersionNumber = Number(plus.os.version.split('.')[0])
console.log(firstVersionNumber);
//判断当前系统版本在8.0及以上
if (firstVersionNumber >= 8) {
Nofi(content)
} else {
try {
isOpenNofiPermission()
//随机生成通知ID
var NotifyID = Math.floor(Math.random() * 10000) + 1; //渠道channel_ID
var main = plus.android.runtimeMainActivity();
var Context = plus.android.importClass("android.content.Context");
var NotificationManager = plus.android.importClass("android.app.NotificationManager");
var nm = main.getSystemService(Context.NOTIFICATION_SERVICE); //通知服务
var Notification = plus.android.importClass("android.app.Notification");
var Intent = plus.android.importClass("android.content.Intent");
var PendingIntent = plus.android.importClass("android.app.PendingIntent");
var intent = new Intent(main, main.getClass());
Notification = plus.android.importClass("android.app.Notification");
mNotification = new Notification.Builder(main);
mNotification.setContentTitle('手动设置的') //设置标题
mNotification.setSubText(payload); //子内容暂时去掉
mNotification.setAutoCancel(true); //设置点击消失
mNotification.setShowWhen(true); //显示通知时间,貌似不加这句也能显示
mNotification.setTicker("PadInfo"); //弹出通知
mNotification.setSmallIcon(17301620); //设置当前app图标
mNotification.setDefaults(Notification.DEFAULT_ALL); //声音、闪灯、震动效果
mNotification.setPriority(Notification.PRIORITY_DEFAULT); //通知优先级
mNotification.flags = Notification.FLAG_ONLY_ALERT_ONCE; //发起通知时震动
mNotification.setContentIntent(pendingIntent);
var mNb = mNotification.build();
} catch (e) {
console.log(e)
}
}
//判断当前系统版本在8.0及以上
if (firstVersionNumber >= 8) {
nm.notify("s" + NotifyID, NotifyID, mNb);
} else {
nm.notify(NotifyID, mNb);
}
plus.device.vibrate(300); //震动
console.log('通知结束');
return true;
// #endif
}
const createNotificationChannel = (channelId, channelName, level = 1) => {
// 获取android版本
let version = Number(plus.os.version.split('.')[0])
const Context = plus.android.importClass('android.content.Context');
if (version > 8) {
var main = plus.android.runtimeMainActivity();
var nm = main.getSystemService(Context.NOTIFICATION_SERVICE); //通知服务
var NotificationChannel = plus.android.importClass('android.app.NotificationChannel');
let channel = new NotificationChannel(channelId, channelName, level)
//设置提示音
channel.setSound();
//开启指示灯
channel.enableLights();
//开启震动
channel.enableVibration();
//设置锁屏展示
channel.setLockscreenVisibility();
channel.setDescription();
nm.createNotificationChannel(channel)
// console.log('channelId是:', channelId)
return channelId
} else {
return null;
}
}
const isOpenNofiPermission = () => {
const main = plus.android.runtimeMainActivity();
var uid = main.getApplicationInfo().plusGetAttribute("uid");
var NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
var pkName = main.getPackageName();
var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
console.error('===通知权限开了吗?', areNotificationsEnabled)
if (!areNotificationsEnabled) {
uni.showModal({
title: '提示',
content: '请先打开APP通知权限',
showCancel: false,
success: function(res) {
if (res.confirm) {
var Intent = plus.android.importClass('android.content.Intent');
var Build = plus.android.importClass("android.os.Build");
//android 8.0已经android8以上版本引导
if (Build.VERSION.SDK_INT >= 26) {
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra("app_package", pkName);
intent.putExtra("app_uid", uid);
} else { //(<21)其他--跳转到该应用管理的详情页
var Settings = plus.android.importClass("android.provider.Settings");
var Uri = plus.android.importClass("android.net.Uri");
var intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", main.getPackageName(), null);
intent.setData(uri);
}
// 跳转到该应用的系统通知设置页
main.startActivity(intent);
}
}
});
}
}
const setClickEvent = () => {
var main = plus.android.runtimeMainActivity();
const Context = plus.android.importClass('android.content.Context');
const Intent = plus.android.importClass('android.content.Intent');
const PendingIntent = plus.android.importClass('android.app.PendingIntent');
var intent = new Intent(main, main.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
let pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
return pendingIntent
}
const Nofi = (content) => {
// 提示权限
isOpenNofiPermission()
// 1==========
let pendingIntent = setClickEvent()
var main = plus.android.runtimeMainActivity();
var NotificationManager = plus.android.importClass("android.app.NotificationManager");
var importance = NotificationManager.IMPORTANCE_HIGH;
let channelId = createNotificationChannel("lidanbao_channel_ID", "理单宝", importance);
let NotificationCompat = plus.android.importClass('androidx.core.app.NotificationCompat')
// 2=============
let builder = new NotificationCompat.Builder(main, channelId)
builder.setContentTitle("您有一条新的消息")
console.log('content是:',content)
builder.setContentText(content)
builder.setContentIntent(pendingIntent)
const android_R = plus.android.importClass(
'android.R'); // android.R:使用系统默认图标,可以把android改成实际项目的包名,则会使用项目的图标
builder.setSmallIcon(17301620)
builder.setPriority(NotificationCompat.PRIORITY_HIGH)
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText("无论是否意识到 Gradle 的存在,每位 Android 程序员都会直接或间接的与 Gradle 打交道。每当通过 Android Studio 新建一个工程时,AS 都会自动创建一个通用的目录结构,然后就可以进行开发,在 app 的 build.gradle 中添加一些依赖,点击右上角的 Sync Now"))
// 3============
var NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
NotificationManagerCompat.from(main).notify(100, builder.build())
}
export {
androidNotify
}
写的有点乱,功能实现了
3 个回复
thomas123
你好,请问楼主的问题解决了吗?我最近也遇到这个问题。
9***@qq.com
遇到同样的问题,一样没反应。官方给解决一下!!!
const doHighLevelNoti = () => {
try {
var channelID = 1
var channelName = "理单宝通知"
var main = plus.android.runtimeMainActivity();
var Context = plus.android.importClass("android.content.Context");
var NotificationManager = plus.android.importClass("android.app.NotificationManager");
var Notification = plus.android.importClass("android.app.Notification");;
}
9***@qq.com
写的有点乱,功能实现了