微信小程序訂閱消息功能


小程序訂閱消息

功能介紹

消息能力是小程序能力中的重要組成,我們為開發者提供了訂閱消息能力,以便實現服務的閉環和更優的體驗。

  • 訂閱消息推送位置:服務通知
  • 訂閱消息下發條件:用戶自主訂閱
  • 訂閱消息卡片跳轉能力:點擊查看詳情可跳轉至該小程序的頁面

使用說明

步驟一:獲取模板 ID
步驟二:獲取下發權限
詳見小程序端消息訂閱接口 wx.requestSubscribeMessage
步驟三:調用接口下發訂閱消息
詳見服務端消息發送接口 subscribeMessage.send
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM