小程序 订阅消息功能实现 wx.requestSubscribeMessage

写在前: 小程序 服务通知 订阅消息功能    小程序文档地址   (前端部分)

第一步,html 内容,添加按钮点击事件

1
2
<text>{{textcontent}}</text>
<button bindtap="subTap" hover-class="other-button-hover">允许订阅消息</button>

第二步,js 内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
data: {
    textcontent: '提示:未开启'
},
  
// 检测是否开启  更新提示
  testingTap: function() {
    let that = this;
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        // console.log(res)
        if (res.subscriptionsSetting.mainSwitch) {
          if (res.subscriptionsSetting.itemSettings != null) {
            let item = res.subscriptionsSetting.itemSettings.P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ
            if (item == "reject") {
              that.setData({
                textcontent: '提示:您已经拒绝订阅消息'
              })
            } else if (item == "accept") {
              that.setData({
                textcontent: '提示:您已经开启订阅消息'
              })
            } else if (item == "ban") {
              that.setData({
                textcontent: '提示:您已经被后台封禁'
              })
            }
          }
        } else {
          that.setData({
            textcontent: '提示:订阅消息未开启'
          })
        }
      }
    })
  },
  
//授权
subTap: function() {
    let that = this;
    wx.requestSubscribeMessage({
      tmplIds: ['P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ'],
      success(res) {
        that.setData({
          textcontent: '提示:授权成功'
        })
      },
      fail(res) {
        that.setData({
          textcontent: '提示:授权失败'
        })
      }
    })
  },

  

posted @   星火卓越  阅读(999)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2017-11-28 JS常用方法
2009-11-28 动态类帮助器
点击右上角即可分享
微信分享提示