1. 程式人生 > >h5+判斷手機通知權限,打開通知權限

h5+判斷手機通知權限,打開通知權限

tac set com text nac console cti del lse

一、蘋果

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();
}

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 {
//針對低版本ios系統
enabledTypes = app.enabledRemoteNotificationTypes();
}
plus.ios.deleteObject(app);
if ( 0 == enabledTypes ) {
uni.showModal({
title: ‘提示‘,
content: ‘請先打開APP通知權限‘,
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(‘已開啟通知權限‘);
}else{
uni.showModal({
title: ‘提示‘,
content: ‘請先打開APP通知權限!‘,
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‘);
//可設置表中所有Action字段
intent.putExtra(‘android.provider.extra.APP_PACKAGE‘, pkName);
main.startActivity(intent);
}
}
});
}

h5+判斷手機通知權限,打開通知權限