场景:uni-app开启移动app,如果用户没开启消息通知提示开启 因为uni-app升级到androidx,之前的android.support.v4.app.NotificationManagerCompat 已经找不到了,androidx中采用androidx.core.app.NotificationManagerCompat
判断是否开启了消息通知
var main = plus.android.runtimeMainActivity();
var pkName = main.getPackageName();
var NotificationManagerCompat = plus.android.importClass(
"androidx.core.app.NotificationManagerCompat");
console.log(NotificationManagerCompat)
var isEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
if (!isEnabled) {
try{
console.log("===================")
}catch(e){
console.log("------------------")
console.log(e)
}
}
当用户确定,调用如下代码,指导用户去开启消息通知
main = plus.android.runtimeMainActivity();
pkName = main.getPackageName();
uid = main.getApplicationInfo().plusGetAttribute("uid");
Intent = plus.android.importClass('android.content.Intent');
Build = plus.android.importClass("android.os.Build");
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) {
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra("app_package", pkName);
intent.putExtra("app_uid", uid);
} else {
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);
|