小程式訂閱消息
功能介紹
消息能力是小程式能力中的重要組成,我們為開發者提供了訂閱消息能力,以便實作服務的閉環和更優的體驗。
- 訂閱消息推送位置:服務通知
- 訂閱消息下發條件:使用者自主訂閱
- 訂閱消息卡片跳轉能力:點選檢視詳情可跳轉至該小程式的頁面
使用說明
步驟一:擷取模闆 ID
步驟二:擷取下發權限
詳見小程式端消息訂閱接口 wx.requestSubscribeMessage
步驟三:調用接口下發訂閱消息
詳見服務端消息發送接口 subscribeMessage.send
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICMyYTMvw1dvwlMvwlM3VWaWV2Zh1Wa-cmbw5yM3kXYxJ2N2dmZvwFO5cTNzQTMtUGall3LcVmdhNXLwRHdo9CXt92YucWbpRWdvx2Yx5yazF2Lc9CX6MHc0RHaiojIsJye.png)
image.png
image.png
uni.requestSubscribeMessage({
tmplIds: [''],
success (res) { }
})
複制
- wx.requestSubscribeMessage(Object object)
- subscribeMessage.send
<form :report-submit="true" @submit="createOrder" style="display: flex; justify-content: center;">
</form>
async createOrder(e) {
try {
await this.joinFormId(e) //收集formId
}
}
payment(info) { //數字現金支付
let params = info.payment
params.success = (res) => {
// #ifdef MP-ALIPAY
if (res.resultCode != 9000 && res.resultCode != 8000) {
uni.showToast({
title: '未支付押金成功!',
icon: 'none'
});
return
}
// #endif
uni.showLoading({
mask: true,
title: '正在處理...'
});
setTimeout(_ => {
uni.hideLoading()
this.goOrder(info.order_sn)
}, 2000);
}
params.fail = (err) => {
uni.showToast({
title: '未支付押金成功!',
icon: 'none'
});
}
uni.requestPayment(params)
},
複制
joinFormId(e) {
return new Promise((resolve, reject) => {
//#ifdef MP-WEIXIN
var wechat_temp = this.$store.state.user.wechat_temp
if (!wx.requestSubscribeMessage) {
resolve()
return
}
wx.requestSubscribeMessage({
tmplIds: [wechat_temp.lend_success, wechat_temp.return_success],
success: (res) => {
console.log('res', res)
const param = {
is_lend: res[wechat_temp.lend_success] === 'accept' ? 1 : 0,
is_return: res[wechat_temp.return_success] === 'accept' ? 1 : 0
}
if (param.is_lend || param.is_return) {
resolve()
subscribeAuthApi(param).then(response => {
console.log('subscribeAuthApi', response)
})
} else {
uni.showModal({
content: '未授權發送通知,将收不到出借、歸還通知',
confirmText: '重新授權',
cancelText: '直接租借',
success: (res) => {
if (res.confirm) {
reject()
this.request_order = false
this.createOrder()
} else {
resolve()
}
}
})
}
},
fail: (err) => {
console.log('ee', err);
uni.showModal({
content: '未授權發送通知,請到小程式設定界面開啟訂閱',
confirmText: '去開啟',
cancelText: '直接租借',
success: (res) => {
if (res.confirm) {
reject()
wx.openSetting()
} else {
resolve()
}
}
})
}
})
//#endif
//#ifdef MP-ALIPAY
console.log('MP-ALIPAY')
const form_id = e.detail.formId
if (form_id.indexOf(' ') !== -1) {
return
}
joinFormIdApi({
form_id
})
resolve()
//#endif
})
},
複制
image.png
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
使用者需要點選“總是保持以上,不再詢問”,才可以擷取到getSetting傳回的使用者授權資訊
image.png
requestMsg(){
return new Promise((resolve, reject) => {
wx.requestSubscribeMessage({
tmplIds: [" -ZwAFL- "],
success: (res) => {
if (res[' -ZwAFL- '] === 'accept'){
wx.showToast({
title: '訂閱OK!',
duration: 1000,
success(data) {
//成功
resolve()
}
})
}
},
fail(err) {
//失敗
console.error(err);
reject()
}
})
})
}
複制
openMsg() {
var that = this
// 擷取使用者的目前設定,判斷是否點選了“總是保持以上,不在詢問”
wx.getSetting({
withSubscriptions:true, // 是否擷取使用者訂閱消息的訂閱狀态,預設false不傳回
success(res) {
if(res.authSetting['scope.subscribeMessage']) { // 使用者點選了“總是保持以上,不再詢問”
uni.openSetting({ // 打開設定頁
success(res) {
console.log(res.authSetting)
}
});
}else { // 使用者沒有點選“總是保持以上,不再詢問”則每次都會調起訂閱消息
var templateid = that.setting.templateid.map(item => item.tempid)
uni.requestSubscribeMessage({
tmplIds: templateid,
success (res) {
console.log(res)
},
fail:(res) => {
console.log(res)
}
})
}
}
})
},
複制
模版消息連結
fromid怎麼成這樣了
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html
image.png