实现效果:
检查安卓和苹果是否具有推送通知权限,若没有权限则跳转到设置页面去
judgePushPermision(function(res){
if(res){
console.log("具有通知权限")
}else{
console.log("不具有通知权限");
toSystemAppPage(); //跳转到设置页面去
}
})
//判断用户是否具有推送通知权限
judgePushPermision(call){
// #ifdef APP-PLUS
let _this = this;
let system = uni.getSystemInfoSync();
if(system.platform == "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 ( enabledTypes == 0 ) {
call && call(false);
}else{
call && call(true);
}
}else{
let main = plus.android.runtimeMainActivity();
let pkName = main.getPackageName();
let NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");/此处注意哈:若hbuilderx版本是使用的3.2.5以前的,则这里引入的包名更换为:android.support.v4.app.NotificationManagerCompat/
let packageNames = NotificationManagerCompat.from(main);
if (packageNames.areNotificationsEnabled()) {
call && call(true);
}else{
call && call(false);
}
}
// #endif
},
//前往系统设置的app页,打开推送
toSystemAppPage(){
// #ifdef APP-PLUS
let _this = this;
let system = uni.getSystemInfoSync();
if(system.platform == "ios"){
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);
}else{
let main = plus.android.runtimeMainActivity();
let pkName = main.getPackageName();
let Intent = plus.android.importClass('android.content.Intent');
let intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');//可设置表中所有Action字段
intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
main.startActivity(intent);
}
// #endif
}
2 个回复
1***@qq.com
难怪我的安卓 突然跳不了了
FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866
查询和获取权限为一体的插件:https://ext.dcloud.net.cn/plugin?id=8674