uni-app开启消息通知

场景: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) {  //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);
    原文作者:红茶两块五
    原文地址: https://blog.csdn.net/weixin_38417148/article/details/121518055
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞