//app.vue 应用启动时调用
push.getClient();
push.init();
//onshow中使用清除角标
plus.push.clear(); //清空通知栏
plus.runtime.setBadgeNumber(0)
获取手机通知权限,打开设置通知栏
安卓8.0以上部分机型需要在通知配置中打开default通知
/**
* 设置手机通知权限
*/
export function setPermissions(resolve = "") {
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");
//android.support.v4升级为androidx
if (NotificationManagerCompat == null) {
NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
}
var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
// 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
if (!areNotificationsEnabled) {
uni.showModal({
title: '通知权限开启提醒',
content: '您还没有开启通知权限,无法接受到消息通知,为了防止错过提现到账通知和佣金结算新增佣金提醒,请前往设置并把所有关于消息推送的权限都设置为开启状态!',
confirmText: '去设置',
cancelText: '取消',
success: function(res) {
if (res.confirm) {
var Intent = plus.android.importClass('android.content.Intent');
var Build = plus.android.importClass("android.os.Build");
var mainActivity = plus.android.runtimeMainActivity();
//android 8.0引导
if (Build.VERSION.SDK_INT >= 26) {
console.log(1)
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
console.log(2)
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra("app_package", pkName);
intent.putExtra("app_uid", uid);
} else { //(<21)其他--跳转到该应用管理的详情页
console.log(3)
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
}
// 跳转到该应用的系统通知设置页
main.startActivity(intent);
}
},
complete() {
if (resolve != '') {
resolve()
}
},
});
} else if (resolve != '') {
resolve()
}
} else if (plus.os.name == 'iOS') { // 判断是ISO
var isOn = undefined;
var types = 0;
var app = plus.ios.invoke('UIApplication', 'sharedApplication');
var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
if (settings) {
types = settings.plusGetAttribute('types');
plus.ios.deleteObject(settings);
} else {
types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
}
plus.ios.deleteObject(app);
isOn = (0 != types);
if (isOn == false) {
uni.showModal({
title: '通知权限开启提醒',
content: '您还没有开启通知权限,无法接受到消息通知,为了防止错过提现到账通知和佣金结算新增佣金提醒,请前往设置并把所有关于消息推送的权限都设置为开启状态!',
confirmText: '去设置',
cancelText: '取消',
success: function(res) {
if (res.confirm) {
var app = plus.ios.invoke('UIApplication', 'sharedApplication');
var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
plus.ios.invoke(app, 'openURL:', setting);
plus.ios.deleteObject(setting);
plus.ios.deleteObject(app);
}
},
complete() {
if (resolve != '') {
resolve()
}
},
});
} else if (resolve != '') {
resolve()
}
}
}
推送工具js,获取设备cid、使用透传时生成本地消息弹窗
安卓离线厂商需要使用clicktype:intent,payload只能在线中使用,点击事件中无法监听到自定义的payload,安卓点击后触发receive监听,需在receive监听中得到payload值。
ios在线 离线都可使用payload自定义消息打开应用,点击事件中可正常监听到自定义payload
离线厂商默认运营消息,限额比较少,需配置好厂商消息分类、申请等。
export default {
init: () => {
plus.push.setAutoNotification(true); //设置通知栏显示通知 //必须设置
plus.push.addEventListener("click", function(msg) {
plus.push.clear(); //清空通知栏
pushHandle(msg) //处理方法
}, false);
// 监听在线消息事件
plus.push.addEventListener("receive", function(res) {
console.log(res)
const messageTitle = res.title;
const messageContent = res.content;
const messagePayload = typeof res.payload == 'string' ?
res.payload : JSON.stringify(res.payload)
if (plus.os.name == 'Android') {
uni.navigateTo({
url: messagePayload
})
} else {
const type = res.type
//【APP离线】收到消息,但没有提醒(发生在一次收到多个离线消息时,只有一个有提醒,但其他的没有提醒)
//【APP在线】收到消息,不会触发系统消息,需要创建本地消息,但不能重复创建
// 必须加msg.type验证去除死循环
if (res.aps == null && type == "receive") {
//创建本地消息,发送的本地消息也会被receive方法接收到,但没有type属性,且aps是null
plus.push.createMessage(messageContent, messagePayload, {
cover: false,
title: messageTitle
});
}
}
});
},
getClient: function getClient(callback) {
plus.push.getClientInfoAsync(info => {
// 如果info不存在,或者info存在,cid不存在则再次获取cid
if (!info || !info.clientid) {
console.log("cid为空=========================================");
if (info && info.clientid) {
uni.setStorageSync('clientid', info.clientid);
callback()
return false;
}
getClient(callback)
} else if (info && info.clientid) {
let cid = info.clientid;
uni.setStorageSync('clientid', cid);
callback()
}
}, function(e) {
console.log('Failed', JSON.stringify(e));
let pinf = plus.push.getClientInfo();
let cid = pinf.clientid; //客户端标识
if (cid) {
uni.setStorageSync('clientid', cid);
callback()
}
})
},
}
const pushHandle = (msg) => {
// console.log(msg)
try {
msg.payload = JSON.parse(msg.payload)
} catch (e) {
msg.payload = msg.payload
}
if (!msg) return false;
uni.navigateTo({
url: msg.payload
})
plus.runtime.setBadgeNumber(0); //清除app角标
}