微信小程序———— 订阅消息功能实现

本文主要介绍下 微信小程序 订阅消息 功能的使用及一些注意事项

1. 首先,需要到小程序后台去添加消息模板

添加模板之后就是使用啦,下发消息是需要用户授权的,来看下API

2. wx.requestSubscribeMessage(Object object)     调起客户端小程序订阅消息界面,如下图所示

代码:

dingyue: function (tmplId){
    let that = this
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log(res)
        if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
          if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tmplId]) {
            let item = res.subscriptionsSetting.itemSettings[tmplId]
            if (item == "reject") {
              that.dingyueComfirm(tmplId)
            } else if (item == "accept") {
              console.log('提示:您已经开启订阅消息')
            } else if (item == "ban") {
              console.log('提示:您已经被后台封禁')
            }
          }else{
            that.dingyueComfirm(tmplId)
          }
        } else {
          that.dingyueComfirm(tmplId)
        }
      }
    })
  },
  dingyueComfirm: function (tmplId){
    wx.showModal({
      title: '订阅消息',
      content: '订阅后,有消息会通过微信通知您',
      success: (res) => {
        if (res.confirm) {
          wx.requestSubscribeMessage({
            tmplIds: [tmplId],
            success: (res) => {
              if (res[tmplId] === 'accept') {
                wx.showToast({
                  title: '订阅成功!',
                  duration: 1000,
                  success(data) {
                    //成功
                    console.error(data);
                  }
                })
              } else if (res[tmplId] == "reject") {
                //引导用户,手动引导用户点击按钮,去设置页开启,## Modals是自定义组件
                wx.showModal({
                  title: '订阅消息',
                  content: '您当前拒绝接受消息通知,是否去开启',
                  confirmText: '开启授权',
                  confirmColor: '#345391',
                  cancelText: '仍然拒绝',
                  cancelColor: '#999999',
                  success: res => {
                    this.globalData.isUpload = true
                    wx.openSetting({
                      success(res) {
                        console.log(res.authSetting)
                        // res.authSetting = {
                        //   "scope.userInfo": true,
                        //   "scope.userLocation": true
                        // }
                      },
                      fail(err) {
                        //失败
                        console.error(err);
                      }
                    })
                  }
                })
              }
            },
            fail(err) {
              //失败
              console.error(err);
            }
          })
        }
      }
    })
  }

首先通过调用wx.getSetting() 获取用户授权列表的授权状态 ,如果拒绝可以申请用户授权,也可以通过wx.openSetting()指引用户去设置页更改设置

注:如果用户没有勾选红框里的内容,那么下次还是需要用户授权才可以下发消息的。

如果用户允许了,那么就可以给用户下发消息了

3. subscribeMessage.send   发送订阅消息

参数都很简单,文档也很清晰。也有示例

data里面的数据对应的就是选用模板里面字段设置的名称

 

4. 上面都没有问题,最后下发消息后微信就可以收到消息啦!如下:

 

好啦,订阅消息功能 就ok啦!

微信小程序中的video组件是用于嵌入视频内容的组件,它提供了丰富的接口和属性来控制视频的播放、格式、布局等。在小程序中使用video组件,可以实现视频的加载、播放、暂停、全屏切换等功能,且支持H.264、H.265视频格式。 要使用video组件,开发者需要在小程序的JSON配置文件中声明对应的权限,例如访问网络权限,以及在WXML中插入video标签并设置其属性。下面是使用video组件的基本步骤和示例代码: 1. 在app.json中声明需要使用的权限: ```json { "permissions": { "scope.userLocation": { "desc": "你的位置信息将用于视频播放" } } } ``` 2. 在WXML页面中使用video组件: ```xml <video src="视频地址" controls="true" autoplay="false" loop="false" muted="false" initial-time="0" poster="封面地址" object-fit="contain" enable-danmu="false" danmu-list="{{danmuList}}" danmu-btn="false" binddanmu="onDanmu" controlslist="nodownload" enable-airplay="false" enable-pip="false" aired-volume="false" muted-volume="false" enable-progress-gesture="false"></video> ``` 3. 在JS中定义danmuList数据和onDanmu事件处理函数: ```javascript Page({ data: { danmuList: [{ text: '弹幕内容', color: '#ff0000', time: 10 }] }, onDanmu: function(e) { console.log(e.detail) } }) ``` 视频播放器组件支持多种事件,如播放事件、暂停事件、时间更新事件等,开发者可以根据需要进行监听和处理。此外,还可以通过编程式接口如`wx.createVideoContext`创建视频上下文,实现更精细的控制。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mickey_于浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值