当前位置:   article > 正文

uniapp消息订阅uni.requestSubscribeMessage

uni.requestsubscribemessage

封装一个订阅消息逻辑组件

现在消息订阅只允许用户点击才能订阅

如果用户没有打开订阅消息的总开关的话,

用户点击也是不会弹出弹框订阅的

弹框使用的uview组件,可以换成其他的

  1. <template>
  2. <!-- 消息订阅组件 -->
  3. <view class="message_subscription">
  4. <button @click="judgementMessageSwitch()" style="margin: 20px;background-color: #FFFFFF;">消息订阅测试</button>
  5. <u-popup v-model="showOpenSettingDialog" mode="center" width="520rpx" :mask-close-able="false" height="300rpx"
  6. border-radius="18">
  7. <div class="mock-dialog">
  8. <div class="mock-dialog-content">
  9. <div class="mock-dialog-title">提示</div>
  10. <div class="mock-dialog-text">
  11. {{tipsContent}}
  12. </div>
  13. </div>
  14. <div class="mock-dialog-button">
  15. <div class="color-gray" @click="dontOpenSetting">{{buttonCancelText}}</div>
  16. <div class="color-blue" @click="openSetting">{{buttonConfirmText}}</div>
  17. </div>
  18. </div>
  19. </u-popup>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'messageSubscription',
  25. components: {},
  26. data() {
  27. return {
  28. showOpenSettingDialog: false, // 授权询问弹框,uview
  29. tipsContent: '为了及时获取订单状态,您是否想接收订单状态的消息提醒?', // 弹框提示内容,
  30. buttonConfirmText: '去开启消息提醒',
  31. buttonCancelText: '不需要提醒',
  32. }
  33. },
  34. methods: {
  35. // 判断消息订阅总开关是否打开
  36. judgementMessageSwitch(flag) {
  37. let that = this
  38. uni.getSetting({
  39. withSubscriptions: true,
  40. success(res) {
  41. console.log(res)
  42. if (!res.subscriptionsSetting.mainSwitch) { // 订阅消息的总开关,如果是关着的,引导用户去打开
  43. that.showOpenSettingDialog = true
  44. } else { // 如果开着,则继续向下打开弹窗,获取用户授权
  45. that.messageSubscription()
  46. }
  47. },
  48. fail() {
  49. that.messageSubscription() // 如果失败,则继续向下打开弹窗,获取用户授权
  50. }
  51. })
  52. },
  53. // 弹窗点订阅,开启消息订阅提醒
  54. openSetting() {
  55. if(this.buttonConfirmText == '确定'){
  56. this.messageSubscription()
  57. return
  58. }
  59. var that = this
  60. uni.openSetting({
  61. withSubscriptions: true,
  62. complete(res) {
  63. uni.getSetting({
  64. withSubscriptions: true,
  65. success(res) {
  66. if (res.subscriptionsSetting.mainSwitch) { // 订阅消息的总开关,如果是开着的
  67. that.tipsContent = '再次点击确定,弹出可订阅列表进行订阅'
  68. that.buttonCancelText = '取消'
  69. that.buttonConfirmText = '确定'
  70. } else {
  71. that.showOpenSettingDialog = false;
  72. }
  73. }
  74. })
  75. }
  76. })
  77. },
  78. // 弹窗点不订阅
  79. dontOpenSetting() {
  80. this.showOpenSettingDialog = false;
  81. },
  82. // 订阅申请弹出,只允许点击弹出
  83. messageSubscription() {
  84. this.tipsContent= '为了及时掌握订单状态,您是否想接收订单状态的消息提醒?' // 弹框提示内容,
  85. this.buttonConfirmText= '去开启消息提醒'
  86. this.buttonCancelText= '不需要提醒'
  87. let that = this
  88. that.showOpenSettingDialog = false;
  89. let tmplId = ['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'] // 模板ID,去小程序后台管理开一个,写进数组里,官方文档也有介绍
  90. uni.requestSubscribeMessage({
  91. tmplIds: tmplId,
  92. success(res) {
  93. console.log("success", res)
  94. if (res['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'] == 'accept') {
  95. console.log('订阅成功');
  96. }
  97. },
  98. })
  99. },
  100. },
  101. }
  102. </script>
  103. <style lang="scss" scoped></style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/720422
推荐阅读
相关标签
  

闽ICP备14008679号