溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

h5+判斷手機(jī)通知權(quán)限,打開通知權(quán)限

發(fā)布時(shí)間:2020-07-09 12:38:00 來源:網(wǎng)絡(luò) 閱讀:1196 作者:HHT15927087748 欄目:開發(fā)技術(shù)

一、蘋果

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 {
//針對(duì)低版本ios系統(tǒng)
enabledTypes = app.enabledRemoteNotificationTypes();
}

console.log("enabledTypes:"+enabledTypes);
if ( 0 == enabledTypes ) {
console.log("在通知欄中開啟消息提示");
}else{
console.log("已開啟");
}

plus.ios.deleteObject(app);

也可以這樣寫:

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 {
//針對(duì)低版本ios系統(tǒng)
enabledTypes = app.enabledRemoteNotificationTypes();
}
plus.ios.deleteObject(app);
if ( 0 == enabledTypes ) {
uni.showModal({
title: '提示',
content: '請(qǐng)先打開APP通知權(quán)限',
showCancel: false,
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);
}
}
});
}

官方示例:
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 {
enabledTypes = app.enabledRemoteNotificationTypes();
}
plus.ios.deleteObject(app);

二、安卓代碼
var main = plus.android.runtimeMainActivity();
var pkName = main.getPackageName();
var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
var packageNames = NotificationManagerCompat.from(main);
console.log(JSON.stringify(packageNames));
if (packageNames.areNotificationsEnabled()) {
console.log('已開啟通知權(quán)限');
}else{
uni.showModal({
title: '提示',
content: '請(qǐng)先打開APP通知權(quán)限!',
showCancel: false,
success: function (res) {
if (res.confirm) {
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
//可設(shè)置表中所有Action字段
intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
main.startActivity(intent);
}
}
});
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI