订阅消息弹窗:
wx.requestSubscribeMessage({ // 订阅弹窗
tmplIds: that.data.tempids,
success(res) {
if (JSON.stringify(res).indexOf("reject") != -1) {
console.log(res, "拒绝")
}
if (JSON.stringify(res).indexOf("accept") != -1) {
console.log(res, "接受")
wx.getSetting({
withSubscriptions: true,
success(res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
// res.subscriptionsSetting = {
// mainSwitch: true, // 订阅消息总开关
// }
console.log(res.subscriptionsSetting)
if (JSON.stringify(res.subscriptionsSetting).indexOf(workTaskTemplateId) != -1) {
console.log("用户选择了“保持以上选择”")
}
}
})
}
},
fail(res) {
console.log(res, '失败');
}
})
属性:
tmplIds (类型 Array) 需要订阅的消息模板的id的集合,一次调用最多可订阅3条消息,每个tmplId对应的模板标题需要不相同,否则会被过滤。
withSubscriptions (默认false) 是否同时获取用户订阅消息的订阅状态,默认不获取。注意: 只返回用户勾选过订阅面板中的“总是保持以上选择,不再询问”的订阅消息。
authSetting 用户授权结果
subscriptionsSetting 用户订阅消息设置,接口参数withSubscriptions
值为true
时才会返回。
mainSwitch 订阅消息总开关,true开启,false关闭,如果为false的时候是调起不了弹窗的(如果关闭了总开关想要再次接收订阅的话可以在本小程序中点右上角三个点的设置,设置里面有一个总开关,打开通知开关,允许通知订阅就行了)
注意:
- 用户勾选 “总是保持以上选择,不再询问” 之后,下次订阅调用 wx.requestSubscribeMessage 不会弹窗,保持之前的选择,修改选择需要打开小程序设置进行修改。
- itemSettings 只返回用户勾选过订阅面板中的“总是保持以上选择,不再询问”的订阅消息。
- 点击拒绝和取消是一样的都是拒绝的意思
常见错误码:
10001 TmplIds can't be empty 参数传空了
10004 Invalid template id 参数类型错误
20001 No template data return, verify the template id exist 没有模板数据,一般是模板 ID 不存在 或者和模板类型不对应 导致的
20003 Templates count out of max bounds 模板消息数量超过上限
20004 The main switch is switched off 用户关闭了主开关,无法进行订阅
20005 This mini program was banned from subscribing messages 小程序被封禁