基本步骤插件市场都有
其他配置阿里云文档也有
以下是uniapp登录时关键代码
// 消息推送
MessagePush() {
// 消息推送
const platform = uni.getSystemInfoSync().platform;
const customerPrefix = this.$store.state.appConfigStore.resInfo.customerPrefix || ''
if (!['ios', 'android'].includes(platform)) return;
const aliyunPush = uni.requireNativePlugin('Aliyun-Push');
const that = this;
// ios
if (platform == 'ios') {
const idResult = aliyunPush.getDeviceId();
aliyunPush.bindAccount({ //一个设备只能绑定一个账号,一个账号被设备绑定也有次数限制
account: customerPrefix + '_' + this.account
}, result1 => {
console.error("苹果bindAccount注册推送", result1)
});
// 应用在前台时,推送通知
aliyunPush.showNoticeWhenForeground({
enable: true
});
aliyunPush.setNotificationCallback({},res=>{
console.error("setNotificationCallback", JSON.stringify(res))
})
aliyunPush.setMessageCallback({}, result => {
console.error("setMessageCallback", JSON.stringify(result))
});
// 注册用户点击推送通知的回调接口
aliyunPush.setNotificationResponseCallback({}, result => {
console.error("setNotificationResponseCallback", JSON.stringify(result))
});
}
// 安卓
if (platform == 'android') {
const channel = uni.requireNativePlugin('Aliyun-Push-NotificationChannel');
const channelRes = channel.isNotificationEnabled({
id: 'test_channel'
});
channel.createChannel({
id: 'test_channel',
name: '测试安卓通知通道',
desc: '测试安卓通知通道',
importance: 3,
});
aliyunPush.registerPush({}, result => {
const event = result.event;
// 厂商通道
const aliyunThirdPush = uni.requireNativePlugin('Aliyun-ThirdPush');
aliyunThirdPush.registerLog({}, result => {
// console.log("third push plugin log : " + result);
})
aliyunThirdPush.registerThirdPush({}, thirdRes => {
const data = JSON.stringify(thirdRes);
console.error("thirdResdata",data)
});
// 注册推送
if (event === 'registerPush') {
if (result.code === 'success') {
console.log("注册推送 成功 ");
const idResult = aliyunPush.getDeviceId();
aliyunPush.bindAccount({ //一个设备只能绑定一个账号,一个账号被设备绑定也有次数限制
account: customerPrefix + '_' + this.account
}, result1 => {
console.error("安卓bindAccount注册推送", result1)
});
} else {
console.error("idResult", aliyunPush.getDeviceId())
console.log("注册推送 " + result.code + " " + result.msg);
}
}
// 打开通知
else if (event === 'onNotificationOpened') {
// 跳转,具体业务
} else {
// console.log("receive push event : " + event);
// console.log("receive push data : " + result);
if (event === 'onNotification') {
const platform = uni.getSystemInfoSync().platform;
//修改角标数量
const badgeNum = that.isShowBadge === true ? data :platform === 'ios' ? 0 : -1
plus.runtime.setBadgeNumber(badgeNum);
}
}
});
}
}
消息推送没成功不要慌,先用阿里云的控制台发消息给app看能不能收到。控制台能收到消息,后端发送收不到大概率后端推送有问题 ,消息没有推到阿里云。
还不行的话查找在阿里云平台查看推送id,应用id,app查看设备id等发送给阿里云消息群(插件市场有群号)技术人员帮忙排查
注意:uniapp打包的时候要注意有没有使用自有证书,如果用的公共测试证书会导致跟华为开发账号的指纹证书不匹配,导致离线推送消息失败