美文网首页
小程序订阅消息开发

小程序订阅消息开发

作者: 小李不小 | 来源:发表于2020-08-27 09:34 被阅读0次

如何调起客户端小程序订阅消息界面,返回用户订阅消息的操作结果。

tmplIds: [id1,id2]是模板消息-数组格式,没有的话,可以去小程序微信公众号后台-订阅消息-我的模板里面去创建。

 wx.requestSubscribeMessage({
              tmplIds: ['0CgtNJBaSmrqp0RildekEy2_YX7r730NK_NbVwA2c-U','gsXEP6txpuZM6FPm0yZotznqIV4TO-8G0KN-QMwJNWA'],
              success (res) {
                  console.log('res----',res)
                  if(res.errMsg=='requestSubscribeMessage:ok'){
                    console.log('订阅成功走实名流程',)
                  }
               },
               fail(err){
     
                wx.showToast({
                  title: `订阅失败${errtext || ''}`,
                  icon: 'none',
                  duration: 2000
                })
             
               }
            })

效果图:这是长期订阅的


image.png
注意:拒绝之后,就无法拉起(不做处理),取消之后,可以再次拉起
-----xhtml
   <button class='btn' bindtap='submit' >
          提交
    </button>

<view class='show-tips'>
 
  <van-popup  show="{{ showCodeTips }}" bind:close="tipsClose">
    <view class='tips-title'>请求授权</view> 
    <view class='tips-code-text'>需要开启您的订阅消息,请打开设置</view>
    <view class='tips-btn'>
      <view bindtap='tipsCodeClose' class='btn-clor wait-see'>取消</view>
      <view class='btn-clor'>
        <button class='allow-btn' bindtap="openSetting">确定</button>
      </view>
    </view>
  </van-popup>
</view>

------js
  submit(){
    const that=this;
    const id='模板id';
    const idtwo='模板id';
    wx.getSetting({
      withSubscriptions: true,
      success (res) {
        const itemSettings=res.subscriptionsSetting.itemSettings;
        console.log('subscriptionsSetting---',res.subscriptionsSetting)
        
        if(res.subscriptionsSetting.mainSwitch == false){  //未开启订阅,去弹窗提示开启订阅
          that.setData({
            showCodeTips:true
          })      
        }else if(itemSettings != undefined){//已经订阅过了
          if(res.subscriptionsSetting.mainSwitch == true && 
            (itemSettings[id]=="accept" || itemSettings[idtwo]=="accept")){
              //已经开启订阅和开启接收订阅消息
              console.log('已经开启了接收订阅和订阅过了,')
          }else if(
            res.subscriptionsSetting.mainSwitch == true &&
            (itemSettings[id]=="reject" && itemSettings[idtwo]=="reject") 
            ){ 
              //开启订阅,拒绝订阅授权
              console.log('开启订阅,拒绝订阅授权')
            }
        }else{ //没有订阅过任何长期消息
          console.log('拉起消息接收订阅',res)
            //已经开启了消息接收订阅,直接弹窗订阅选择授权
            wx.requestSubscribeMessage({
              tmplIds: ['id1','idtwo'],
              success (res) {
                  console.log('res----',res)
                  if(res.errMsg=='requestSubscribeMessage:ok'){
                    console.log('',)
                  }
               },
               fail(err){
                 let errtext;
                  if(err.errCode){
                    errtext=`失败状态码:${err.errCode}`
                  }
                wx.showToast({
                  title: `订阅失败,请联系管理员,${errtext || ''}`,
                  icon: 'none',
                  duration: 2000
                })
                console.log('err---',err)
                console.log('订阅成功走实名流程',)
               }
            })
        }
       
      }
    })
  },

如何判断当前是否开了接收消息 ,和用户已经订阅过了就,就需要用到getSetting 必须带 withSubscriptions:true属性

1 mainSwitch: true, // 订阅消息总开关 (用户在设置中已经开了订阅就为true,反之未false)

1 下面图片 mainSwitch: true 的状态
image.png
2 下面图片 mainSwitch: false的状态
image.png

SYS_MSG_TYPE_INTERACTIVE: 'accept' 订阅成功返回状态

zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 拒绝订阅放回的状态

ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban', 后台禁用返回的状态

wx.getSetting({
  withSubscriptions: true,
  success (res) {
    console.log(res.authSetting)
    // res.authSetting = {
    //   "scope.userInfo": true,
    //   "scope.userLocation": true
    // }
    console.log(res.subscriptionsSetting)
    // res.subscriptionsSetting = {
    //   mainSwitch: true, // 订阅消息总开关
    //   itemSettings: {   // 每一项开关
    //     SYS_MSG_TYPE_INTERACTIVE: 'accept', // 小游戏系统订阅消息
    //     SYS_MSG_TYPE_RANK: 'accept'
    //     zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 普通一次性订阅消息
    //     ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
    //   }
    // }
  }
})

相关文章

  • 小程序订阅消息推送(含源码)java实现小程序推送,spring

    前面写过一篇云开发实现小程序订阅消息(模板消息)推送的文章,《借助云开发实现小程序订阅消息和模板消息的推送功能》是...

  • 小程序订阅消息开发

    如何调起客户端小程序订阅消息界面,返回用户订阅消息的操作结果。 tmplIds: [id1,id2]是模板消息-数...

  • 微信小程序订阅消息功能

    小程序订阅消息 功能介绍 消息能力是小程序能力中的重要组成,我们为开发者提供了订阅消息能力,以便实现服务的闭环和更...

  • 微信小程序订阅消息开发之云开发(二)

    这一篇主要写怎么推送订阅消息给用户,上一篇写了怎么实现订阅消息,没看的可以先看下~微信小程序订阅消息开发之云开发(...

  • 【干货】快速接入小程序订阅消息(附源代码)

      这是一份小程序消息订阅快速开发指引,其中演示了如何快速上手消息订阅的订阅、推送、支付订阅功能。   根据官方指...

  • 小程序消息:小程序订阅消息

    功能介绍 消息能力是小程序能力中的重要组成,我们为开发者提供了订阅消息能力,以便实现服务的闭环和更优的体验。 订阅...

  • 微信小程序的订阅消息

    介绍 微信小程序的订阅消息,是指开发者可以发送消息到用户的微信服务通知中。订阅消息分为 一次性订阅消息 和 长期订...

  • 小程序消息订阅

    开发文档https://developers.weixin.qq.com/miniprogram/dev/api/...

  • 小程序订阅消息

    完善小程序类型的时候,会决定我们所能选用的消息模板,选择需要的模板,复制对应的服务ID在订阅的时候使用(这个操作是...

  • 微信小程序:订阅消息

    1. 什么是小程序订阅消息 用户在小程序页面点击按钮,确认订阅消息,小程序可以按照消息模板格式在后续的任意时间发送...

网友评论

      本文标题:小程序订阅消息开发

      本文链接:https://www.haomeiwen.com/subject/iioqsktx.html