Some checks failed
Uniapp 自动化打包 CI/CD / 打包 Uniapp 项目 (push) Has been cancelled
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
export default {
|
|
// 检查通知权限
|
|
checkPermission(callback) {
|
|
if (plus.os.name !== "Android") {
|
|
callback(true) // iOS 这里直接返回 true
|
|
return
|
|
}
|
|
|
|
try {
|
|
let NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat")
|
|
let context = plus.android.runtimeMainActivity()
|
|
let manager = NotificationManagerCompat.from(context)
|
|
callback(manager.areNotificationsEnabled())
|
|
} catch (e) {
|
|
console.error("检查通知权限出错:", e)
|
|
callback(false)
|
|
}
|
|
},
|
|
|
|
// 打开通知权限设置页面
|
|
openPermissionSetting() {
|
|
if (plus.os.name !== "Android") return
|
|
|
|
try {
|
|
const main = plus.android.runtimeMainActivity()
|
|
const Intent = plus.android.importClass('android.content.Intent')
|
|
const Settings = plus.android.importClass('android.provider.Settings')
|
|
const Uri = plus.android.importClass('android.net.Uri')
|
|
const Build = plus.android.importClass('android.os.Build')
|
|
|
|
let intent
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
// Android 8.0 及以上
|
|
intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, main.getPackageName())
|
|
} else if (Build.VERSION.SDK_INT >= 21) {
|
|
// Android 5.0 - 7.1
|
|
intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
|
intent.putExtra("app_package", main.getPackageName())
|
|
intent.putExtra("app_uid", main.getApplicationInfo().uid)
|
|
} else {
|
|
// Android 4.4 及以下,跳转到应用详情页
|
|
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
|
intent.setData(Uri.fromParts("package", main.getPackageName(), null))
|
|
}
|
|
|
|
main.startActivity(intent)
|
|
} catch (e) {
|
|
console.error("跳转通知设置页失败:", e)
|
|
}
|
|
},
|
|
|
|
// 统一方法:检查并申请
|
|
ensurePermission(next) {
|
|
this.checkPermission((enabled) => {
|
|
if (enabled) {
|
|
next && next() // 已有权限,继续逻辑
|
|
} else {
|
|
uni.showModal({
|
|
title: "通知权限未开启",
|
|
content: "请开启通知权限以接收消息提醒",
|
|
confirmText: "去开启",
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
console.log('t1', '11111111')
|
|
this.openPermissionSetting()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
} |