// 是否开启允许通知
isNotificationsEnabled() {
// #ifdef APP-PLUS
if(plus.os.name == 'Android' ) {
var main = plus.android.runtimeMainActivity();
var pkName = main.getPackageName();
var uid = main.getApplicationInfo().plusGetAttribute( "uid" );
var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
// 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
if (!areNotificationsEnabled) {
uni.showModal({
title: '提示',
content: '请打开APP通知权限,否则您可能不能及时收到我们的通知消息',
success(res) {
if (res.confirm) {
var Intent = plus.android.importClass('android.content.Intent');
var Build = plus.android.importClass("android.os.Build");
//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)其他--跳转到该应用管理的详情页
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
}
// 跳转到该应用的系统通知设置页
main.startActivity(intent);
}
}
});
}
} else if(plus.os.name == '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");
} else {
//针对低版本ios系统
enabledTypes = app.enabledRemoteNotificationTypes();
}
plus.ios.deleteObject(app);
if ( 0 == enabledTypes ) {
uni.showModal({
title: '提示',
content: '请打开APP通知权限,否则您可能不能及时收到我们的通知消息',
success: function (res) {
if (res.confirm) {
var UIApplication = plus.ios.import("UIApplication");
var NSURL = plus.ios.import("NSURL");
var setting = NSURL.URLWithString("app-settings:");
var application = UIApplication.sharedApplication();
application.openURL(setting);
plus.ios.deleteObject(setting);
plus.ios.deleteObject(application);
}
}
});
}
}
// #endif
}
1 个回复
chenguokai (作者)