当前位置:网站首页>【微信小程序开发(七)】订阅消息
【微信小程序开发(七)】订阅消息
2022-07-28 14:49:00 【袜子是一只狗】
订阅消息

一、小程序订阅消息逻辑处理
可以将其挂在APP上,使用起来会更加便捷
subscribeMessage: (val, callback) => {
//需要订阅的消息模板,在微信公众平台手动配置获取模板ID
let message = val; // 模板id
//如果总是拒绝(subscriptionsSetting,2.10.1库才支持)
// wx.setStorageSync("subType",1)
if (versionCompare('2.10.1')) {
wx.getSetting({
withSubscriptions: true,//是否同时获取用户订阅消息的订阅状态,默认不获取
success: (res) => {
console.info("res",res)
if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings &&
res.subscriptionsSetting.itemSettings[message] == "reject") {
//打开设置去设置
openConfirm('检测到您没打开推送权限,是否去设置打开?');
} else if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings &&
res.subscriptionsSetting.itemSettings[message] == "accept") {
// 已有授权过
console.log('已有订阅授权过')
wx.requestSubscribeMessage({
tmplIds: message,
success: (res) => {
if (res[message] == 'accept') {
console.log("resrequestSubscribeMessage",res)
//用户允许
}
callback()
},
fail: (res) => {
console.log("res",res)
callback()
},
})
} else {
wx.requestSubscribeMessage({
tmplIds: message,
success: (res) => {
if (res[message] == 'accept') {
console.log("resrequestSubscribeMessage",res)
//用户允许
}
callback()
},
fail: (res) => {
console.log("res",res)
callback()
},
})
}
}
})
} else if (versionCompare('2.4.4')) {
wx.requestSubscribeMessage({
tmplIds: message,
success: (res) => {
if (res[message] == 'accept') {
//用户允许
}
callback()
},
fail: (res) => {
console.info(res)
callback()
},
})
}
//打开设置
function openConfirm(message) {
wx.showModal({
content: message,
confirmText: "确认",
cancelText: "取消",
success: (res) => {
//点击“确认”时打开设置页面
if (res.confirm) {
wx.openSetting({
success: (res) => {
console.log(res.authSetting)
},
fail: (error) => {
console.log(error)
}
})
} else {
console.log('用户点击取消')
}
}
});
}
//基础库版本比较
function versionCompare(v) {
const version = wx.getSystemInfoSync().SDKVersion
if (compareVersion(version, v) >= 0) {
return true
} else {
return false
}
}
function compareVersion(v1, v2) {
v1 = v1.split('.')
v2 = v2.split('.')
var len = Math.max(v1.length, v2.length)
while (v1.length < len) {
v1.push('0')
}
while (v2.length < len) {
v2.push('0')
}
for (var i = 0; i < len; i++) {
var num1 = parseInt(v1[i])
var num2 = parseInt(v2[i])
if (num1 > num2) {
return 1
} else if (num1 < num2) {
return -1
}
}
return 0
}
},
二、使用步骤
app.subscribeMessage(['这里放模板ID'],()=>{
// 订阅后的回调方法
});
三、不订阅也能收到一次性模板消息?
用户点击一次就相当于订阅了一次,相当于你拥有了一次获取系统通知的门票,理论上用户多次点击就相当于订阅了多次,此时是会将这些订阅次数储存起来,如果服务器下发通知时就会消耗一次订阅次数(实际是这样的,文档没有说,需要确保每次下发前触发一次订阅交互);产品是可以接受的。不是bug.
边栏推荐
- 1路编码器2路DI转速测量RS485串口连接1路DO报警模块IBF151
- Deutsche Telekom denied strengthening its cooperation with Huawei and said it had reduced its cooperation with Huawei in the past three years
- 如何搭建openGrok代码服务器
- 热敏电阻PT100,NTC转0-10V/4-20mA转换器
- 屏下指纹价格战再起,二线厂商今年有望拿下30%市场?
- 记:数字累加动画
- Give you a linked list, delete the penultimate node of the linked list, and return the head node of the linked list.
- 如何压缩与解压缩ramdisk.img
- Several slips of X rust, those things that have to be said
- Pytorch - optimize model parameters
猜你喜欢

电压转电流/电流转电压模块

Docker container implements MySQL master-slave replication

Getting started with crawlers (1) -- requests (1)

软件架构与设计(八)-----分布式架构

生命的感悟

800V high voltage system

How as makes intelligent prompts regardless of case

活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!

Solve the problem that the right-click menu "edit with idle" of the 『 py 』 file is invalid or missing

5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95
随机推荐
10. Implementation of related data accumulation task
玩死原型链
占空比开关量输出高速脉冲计数器RTUModbus模块IBF63
为赴美上市做准备?Arm宣布将剥离物联网服务业务:未来将聚焦芯片底层设计
About how Simulink generates model coverage reports
How to turn on and off flight mode through ADB
PXE网络装机
Preparing for listing in the United States? Arm announced that it would divest the Internet of things service business: the future will focus on the underlying chip design
Learn about the native application management platform of rainbow cloud
8. Realization of real-time data backup and real-time clock function
低成本/小体积模块RS485/232转模拟信号的原理以及应用IBF33
Learn RX programming from me -- concat
Daily news on July 28, 2022: Science: AI has made another breakthrough in protein design, and can design specific functional proteins
如何通过adb打开和关闭飞行模式
Voice social system -- improve the audio system industry chain
编码器高速脉冲计数器Modbus RTU模块IBF150
一次失败的破解经历
Software architecture and design (V) -- data centric architecture
Late 2021 year-end summary
1200 times faster! MIT develops a new generation of drug research and development AI, and suspends the old model