uniapp 小程序订阅消息爬坑记

https://blog.csdn.net/weixin_42460570/article/details/103347322html

https://www.jianshu.com/p/0077a8084b44小程序

1、选择合适的模板消息微信

在小程序管理后台功能-订阅消息-公共模板库选择本身须要的模板消息。目前小程序支持一次性订阅和长期订阅,一次性订阅是指用户订阅一次,能够为其推送一次消息;长期订阅则是能够发送屡次消息,目前向政务、医疗、交通、金融、教育等线下公共服务开放,若是有须要能够在社区发帖申请,符合要求应该会给开放(这个我没申请过经过率不肯定哈)。微信开发

核心代码app

wx.requestSubscribeMessage({
  tmplIds: [''],
  success (res) { }
})
<button bindtap="subscribeMsg">点击订阅消息</button>

 // 订阅消息
    subscribeMsg() {
        let that = this
        let tmplId = ['gh8CVR5Qn0-an']; //['gh8CVR5Qn0-an','YqJnLuXMt7027NAEBB0p'] 一条或者多条
        //var template_ids = app.globalData.tmplIds;
        wx.requestSubscribeMessage({
            tmplIds: tmplId,//template_ids
            success(res) {
                if (res[tmplId] == 'accept') { //某条订阅信息 接收或者拒绝
                    that.cloudSendMsg();
                } else if (res[tmplId] == 'reject') { // 用户拒绝受权
                    wx.showModal({
                        title: '舒适提示',
                        content: "您已关闭消息推送,如须要消息推送服务,请点击肯定跳转设置页面打开受权后再次尝试。",
                        success: function(modal) {
                            if (modal.confirm) { // 点击肯定
                                wx.openSetting({ withSubscriptions: true })
                            }
                        }
                    })
                }
            },
            fail(err) {
                if (err.errCode == '20004') {
                    wx.showModal({
                        title: '温馨提示',
                        content: "您的消息订阅主开关已关闭,如须要消息推送服务,请点击肯定跳转设置页面打开受权后再次尝试。",
                        success: function(modal) {
                            if (modal.confirm) { // 点击肯定
                                wx.openSetting({ withSubscriptions: true })
                            }
                        }
                    })
                }
            },
           complete(res) {
            console.log('complete  调用完成')
            // 不管取消仍是接收都会执行:好比 下单(不管订阅号是取消仍是接收都执行)
            this.pay()
          }
        })
    },
调用失败fail返回提示及解决方式:

1.errMsg:"requestSubscribeMessage:fail can only be invoked by user TAP gesture."工具

解决:bindtap点击才行、不能延迟调用this

2.errMsg:"requestSubscribeMessage:fail 开发者工具暂时不支持此 API 调试,请使用真机进行开发".net

解决:真机调试才行调试

3.errMsg:"requestSubscribeMessage:fail:No template data return, verify the template id exist"code

   errorCode:20001

解决:确认模板ID是订阅消息的模板ID且正确。

4.errMsg:"requestSubscribeMessage:fail:Templates count out of max bounds"

   errCode:20003

解决:模版数量超出,最多3个

5.requestSubscribeMessage:fail last call has not ended

解决:若是申请的是“一次性订阅”,一次触发只有一次推送消息机会,因此咱们会在其余页面点击事件去调用requestSubscribeMessage,获取更屡次的推送消息机会。可是每一个手机获取是有上限的,大概几十个吧。而后只有用掉推送消息次数,才能从新成功调用,不然会出现以上问题。

注意-坑:

1.bindtap点击才能够调起

2.真机调试才行(--2020/01/03:貌似微信开发者工具也能够调用了)

3.不能延迟去调用wx.requestSubscribeMessage,如先调用其余接口请求成功再去它、使用定时器延迟调用

4.目前长期性订阅消息仅向政务民生、医疗、交通、金融、教育等线下公共服务开放,后期将逐步支持到其余线下公共服务业务

本文来自博客园,作者:💞Travelerᘗ,转载请注明原文链接:https://www.cnblogs.com/LindaBlog/p/15818336.html

原文地址:https://www.cnblogs.com/LindaBlog/p/15818336.html