h5+ 检测 APP 是否开启应用通知权限
原文可查看此处,搜索 h5+ 检测 APP 是否开启应用通知权限
https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg3NTAzMzAxNA==&scene=124#wechat_redirect
如果此链接在浏览器中无法打开,可将此链接发送给微信好友或者发送到微信文件传输助手。在点击打开
——————————————————————————————————————————
前置条件:
开发环境:windows
开发框架:uni-app , H5+
编辑器:HbuilderX
兼容版本:安卓5.0~9.0版本,IOS未作测试
此代码可以直接复制到uni-app项目中使用
// 判断App是否打开了通知权限
noticeIsOpen(){
switch (uni.getSystemInfoSync().platform){
//判断安卓是否开通应用通知权限
case 'android':
var main = plus.android.runtimeMainActivity();
var pkName = main.getPackageName();
var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
var packageNames = NotificationManagerCompat.from(main);
//手机没有开启通知的权限
if (!packageNames.areNotificationsEnabled()) {
var uid = main.getApplicationInfo().plusGetAttribute("uid");
var Intent = plus.android.importClass('android.content.Intent');
var Build = plus.android.importClass("android.os.Build");
uni.showModal({
title: '提示',
content: '检测到地毯汇应用未开启通知权限,请开启通知权限',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
//android 8.0引导
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);
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
break;
//判断IOS是否开通应用通知权限
case 'ios':
// 识别IOS通知权限是否打开
var UIApplication = plus.ios.import("UIApplication");
var app = UIApplication.sharedApplication();
var enabledTypes = 0;
if (app.currentUserNotificationSettings) {
var settings = app.currentUserNotificationSettings();
enabledTypes = settings.plusGetAttribute("types");
console.log("enabledTypes1:" + enabledTypes);
if (enabledTypes == 0) {
plus.nativeUI.confirm("推送设置没有开启,是否去开启?", function(e) {
if (e.index == 0) {
var NSURL2 = plus.ios.import("NSURL");
var setting2 = NSURL2.URLWithString("app-settings:");
var application2 = UIApplication.sharedApplication();
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}
}, {
"buttons": ["Yes", "No"],
"verticalAlign": "center"
});
}
plus.ios.deleteObject(settings);
} else {
enabledTypes = app.enabledRemoteNotificationTypes();
if(enabledTypes == 0){
console.log("推送未开启!");
}else{
console.log("已经开启推送功能!")
}
console.log("enabledTypes2:" + enabledTypes);
}
plus.ios.deleteObject(app)
break;
default:
break;
}
}