辛德瑞拉
辛德瑞拉
  • 发布:2018-09-20 15:16
  • 更新:2019-09-06 20:11
  • 阅读:2976

安卓怎么获取是否开启系统通知

分类:Native.js

ios 可以获取到系统通知是开启还是关闭,安卓怎么获取系统的消息通知是否为开启状态呢,如果没有开启,跳转到系统设置,开启消息通知的代码又是怎么样的呢?求助各种大佬,

2018-09-20 15:16 负责人:无 分享
已邀请:
辛德瑞拉

辛德瑞拉 (作者) - js 也会 php 也会 但是都是皮毛,,,

我的意思不是这个。。。我的意思是手机不是有推送,个推的推送,用户或者手机会默认把推送功能关闭,所以我需要检测一下,用户的手机是否开启了接收个推推送的通知,如果没有开启,跳转到当前应用设置开启通知的界面,ios已经实现,安卓不知道怎么实现,能加下你,吗,或者你加下我也可以,我qq254228737 微信的话搜索254228737。

fx4399

fx4399 - Android工程师、前端工程师

列如 设置网络

var main = plus.android.runtimeMainActivity(); //获取activity  
                var Intent = plus.android.importClass('android.content.Intent');  
                var Settings = plus.android.importClass('android.provider.Settings');  
                var intent = new Intent(Settings.ACTION_WIFI_SETTINGS);//显示设置,以允许WiFi配置  
                main.startActivity(intent);

Android平台 Native.js跳转系统设置各个界面

  • 辛德瑞拉 (作者)

    你这个是跳转到系统设置界面呢,我也没找到直接跳转到设置通知的action名称,能不能请教下,怎么获取用户是不是开启了通知,

    2018-09-20 16:05

  • fx4399

    1.刚才那个列子就判断下当前是否有网络 plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE 这句代码代表没有网络

    2.可以在manifest里设置"fullscreen": false就可以显示通知栏,或者在某个页面写plus.navigator.setFullscreen(false)也可以显示这个页面的通知栏。

    2018-09-20 16:50

  • 辛德瑞拉 (作者)

    回复 fx4399:回复在下面,我传了个图片

    2018-09-21 09:33

5***@qq.com

5***@qq.com

有解决方案了么

chenguokai

chenguokai

额,我是ios的还不知道怎么跳转,Android的已经实现了

chenguokai

chenguokai

代码如下

// 是否开启通知权限  
            requestPermission() {    
                // #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') {  

                }  
                // #endif  
            } 
  • chenguokai

    谁有ios跳转的方法贴一个,我用下,赫赫

    2019-08-06 09:51

  • chenguokai

    一样是因为推送功能去找怎么跳转到通知设置页面,呜呜,找了好久

    2019-08-06 09:56

  • 5***@qq.com

    回复 chenguokai: 你看下我回复的

    2019-09-06 20:09

  • 7***@qq.com

    回复 chenguokai: android.support.v4包是什么版本的,我的报错Uncaught Attempt to invoke virtual method 'java.lang.String java.lang.Class.getName()' on a null object reference;at importClass android.support.v4.app.NotificationManagerCompat

    2019-09-22 16:40

  • 爱祺

    回复 chenguokai: 你好大神 我运行的这个值areNotificationsEnabled 为什么不管事拒绝了还是允许了 都为true呢

    2020-04-06 21:50

5***@qq.com

5***@qq.com

async getPushState() {
let pushTag = false
if (this.isIos) {
pushTag = await permision.judgeIosPermission("push") //判断iOS上是否给予推送权限,有权限返回true,否则返回false
this.pushFlag = pushTag
}
}

5***@qq.com

5***@qq.com

/**

  • 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
    */

var isIos
// #ifdef APP-PLUS
isIos = (plus.os.name == "iOS")
// #endif

// 判断推送权限是否开启
function judgeIosPermissionPush() {
var result = false;
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) {
console.log("推送权限没有开启");
} else {
result = true;
console.log("已经开启推送功能!")
}
plus.ios.deleteObject(settings);
} else {
enabledTypes = app.enabledRemoteNotificationTypes();
if (enabledTypes == 0) {
console.log("推送权限没有开启!");
} else {
result = true;
console.log("已经开启推送功能!")
}
console.log("enabledTypes2:" + enabledTypes);
}
plus.ios.deleteObject(app);
plus.ios.deleteObject(UIApplication);
return result;
}

// 判断定位权限是否开启
function judgeIosPermissionLocation() {
var result = false;
var cllocationManger = plus.ios.import("CLLocationManager");
var status = cllocationManger.authorizationStatus();
result = (status != 2)
console.log("定位权限开启:" + result);
// 以下代码判断了手机设备的定位是否关闭,推荐另行使用方法 checkSystemEnableLocation
/ var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
console.log("enable:" + enable);
console.log("status:" + status);
if (enable && status != 2) {
result = true;
console.log("手机定位服务已开启且已授予定位权限");
} else {
console.log("手机系统的定位没有打开或未给予定位权限");
}
/
plus.ios.deleteObject(cllocationManger);
return result;
}

// 判断麦克风权限是否开启
function judgeIosPermissionRecord() {
var result = false;
var avaudiosession = plus.ios.import("AVAudioSession");
var avaudio = avaudiosession.sharedInstance();
var permissionStatus = avaudio.recordPermission();
console.log("permissionStatus:" + permissionStatus);
if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
console.log("麦克风权限没有开启");
} else {
result = true;
console.log("麦克风权限已经开启");
}
plus.ios.deleteObject(avaudiosession);
return result;
}

// 判断相机权限是否开启
function judgeIosPermissionCamera() {
var result = false;
var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
console.log("authStatus:" + authStatus);
if (authStatus == 3) {
result = true;
console.log("相机权限已经开启");
} else {
console.log("相机权限没有开启");
}
plus.ios.deleteObject(AVCaptureDevice);
return result;
}

// 判断相册权限是否开启
function judgeIosPermissionPhotoLibrary() {
var result = false;
var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
var authStatus = PHPhotoLibrary.authorizationStatus();
console.log("authStatus:" + authStatus);
if (authStatus == 3) {
result = true;
console.log("相册权限已经开启");
} else {
console.log("相册权限没有开启");
}
plus.ios.deleteObject(PHPhotoLibrary);
return result;
}

// 判断通讯录权限是否开启
function judgeIosPermissionContact() {
var result = false;
var CNContactStore = plus.ios.import("CNContactStore");
var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
if (cnAuthStatus == 3) {
result = true;
console.log("通讯录权限已经开启");
} else {
console.log("通讯录权限没有开启");
}
plus.ios.deleteObject(CNContactStore);
return result;
}

// 判断日历权限是否开启
function judgeIosPermissionCalendar() {
var result = false;
var EKEventStore = plus.ios.import("EKEventStore");
var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
if (ekAuthStatus == 3) {
result = true;
console.log("日历权限已经开启");
} else {
console.log("日历权限没有开启");
}
plus.ios.deleteObject(EKEventStore);
return result;
}

// 判断备忘录权限是否开启
function judgeIosPermissionMemo() {
var result = false;
var EKEventStore = plus.ios.import("EKEventStore");
var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
if (ekAuthStatus == 3) {
result = true;
console.log("备忘录权限已经开启");
} else {
console.log("备忘录权限没有开启");
}
plus.ios.deleteObject(EKEventStore);
return result;
}

// Android权限查询
function requestAndroidPermission(permissionID) {
return new Promise((resolve, reject) => {
plus.android.requestPermissions(
[permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
function(resultObj) {
var result = 0;
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i];
console.log('已获取的权限:' + grantedPermission);
result = 1
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i];
console.log('拒绝本次申请的权限:' + deniedPresentPermission);
result = 0
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i];
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
result = -1
}
resolve(result);
// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
// if (result != 1) {
// gotoAppPermissionSetting()
// }
},
function(error) {
console.log('申请权限错误:' + error.code + " = " + error.message);
resolve({
code: error.code,
message: error.message
});
}
);
});
}

// 使用一个方法,根据参数判断权限
function judgeIosPermission(permissionID) {
if (permissionID == "location") {
return judgeIosPermissionLocation()
} else if (permissionID == "camera") {
return judgeIosPermissionCamera()
} else if (permissionID == "photoLibrary") {
return judgeIosPermissionPhotoLibrary()
} else if (permissionID == "record") {
return judgeIosPermissionRecord()
} else if (permissionID == "push") {
return judgeIosPermissionPush()
} else if (permissionID == "contact") {
return judgeIosPermissionContact()
} else if (permissionID == "calendar") {
return judgeIosPermissionCalendar()
} else if (permissionID == "memo") {
return judgeIosPermissionMemo()
}
return false;
}

// 跳转到应用的权限页面
function gotoAppPermissionSetting() {
if (isIos) {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
var setting2 = NSURL2.URLWithString("app-settings:");
application2.openURL(setting2);

    plus.ios.deleteObject(setting2);  
    plus.ios.deleteObject(NSURL2);  
    plus.ios.deleteObject(application2);  
} else {  
    // console.log(plus.device.vendor);  
    var Intent = plus.android.importClass("android.content.Intent");  
    var Settings = plus.android.importClass("android.provider.Settings");  
    var Uri = plus.android.importClass("android.net.Uri");  
    var mainActivity = plus.android.runtimeMainActivity();  
    var intent = new Intent();  
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  
    var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);  
    intent.setData(uri);  
    mainActivity.startActivity(intent);  
}  

}

// 检查系统的设备服务是否开启
// var checkSystemEnableLocation = async function () {
function checkSystemEnableLocation() {
if (isIos) {
var result = false;
var cllocationManger = plus.ios.import("CLLocationManager");
var result = cllocationManger.locationServicesEnabled();
console.log("系统定位开启:" + result);
plus.ios.deleteObject(cllocationManger);
return result;
} else {
var context = plus.android.importClass("android.content.Context");
var locationManager = plus.android.importClass("android.location.LocationManager");
var main = plus.android.runtimeMainActivity();
var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
console.log("系统定位开启:" + result);
return result
}
}

module.exports = {
judgeIosPermission: judgeIosPermission,
requestAndroidPermission: requestAndroidPermission,
checkSystemEnableLocation: checkSystemEnableLocation,
gotoAppPermissionSetting: gotoAppPermissionSetting
}

要回复问题请先登录注册