【小程序】fail can only be invoked by user TAP gesture 唤起订阅消息多端兼容解决方案

这篇具有很好参考价值的文章主要介绍了【小程序】fail can only be invoked by user TAP gesture 唤起订阅消息多端兼容解决方案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在对接消息订阅功能时,出现了调用 uni.requestSubscribeMessage 后无法唤起订阅消息窗口的情况。

支付宝、微信小程序的行为

uni.requestSubscribeMessage 之前存在异步逻辑时无法唤起,但是在 uni.showModal 的回调中调用时可以唤起。

抖音、快手小程序的行为

抖音小程序中 uni.requestSubscribeMessage 事件必须手动点击直接触发,不能在回调中触发:见 tt.requestSubscribeMessage 返回错误 中的评论。

在抖音中必须在点击事件中直接调用 uni.requestSubscribeMessage 才可以唤起窗口,并且之前不能存在异步逻辑,因此如果模板 id 是通过后端接口获取的,就要在点击事件之前先获取到模板 id 才可以。

uni.requestSubscribeMessage 之前存在异步逻辑时无法唤起,在 uni.showModal 的回调中调用时也不能唤起。

解决方案

对于上述的问题,一个解决的思路是自己封装一个 modal 组件,而不是直接用小程序自带的 uni.showModal。在初始化 modal 组件时异步获取模板 id,在点击事件中调用 uni.requestSubscribeMessage,这样就可以统一多端的发起订阅消息请求逻辑。

需要注意的是必须直接在点击事件中调用,否则抖音和快手依然是不生效的。

  • 订阅消息组件:
<template>
  <HmModal :isShowModal="isShowRequestMessageModal" @closeModal="emitCancel">
    <view slot="body"> 为了通知您办理进度,需要向您推送相关消息 </view>
    <view slot="footer" class="c-modal-footer">
      <view class="c-modal-footer-btn cancel" @click="emitCancel">取消</view>
      <view class="c-modal-footer-btn confirm" @click="confirm">确认</view>
    </view>
  </HmModal>
</template>

<script>
// 抖音、快手需要由点击事件直接触发,不能在 uni.showModal 的回调中触发
import HmModal from "@/components/modal/HmModal";
import SubscribeService from "@/services/bussiness/subscribe/SubscribeService";
import Env from "@/env/Env";

export default {
  components: {
    HmModal,
  },
  data() {
    return {
      templIds: null,
    };
  },
  props: ["isShowModal"],
  computed: {
    isShowRequestMessageModal() {
      return this.templIds && this.templIds.length > 0 && this.isShowModal;
    },
  },
  methods: {
    async confirm() {
      await SubscribeService.requestSubscribeMessageByTmplIds(this.templIds);
      this.emitConfirm();
    },
    emitConfirm() {
      this.$emit("confirm");
    },
    emitCancel() {
      this.$emit("cancel");
    },
    emitNoTemplate() {
      this.$emit("not-support");
    },
  },
  async created() {
    if (Env.PLATFORM.ALIPAY) {
      this.emitNoTemplate();
    } else {
      this.templIds = await SubscribeService.getTemplIds().catch(console.error);
      if (!this.templIds || this.templIds.length === 0) {
        this.emitNoTemplate();
      }
    }
  },
};
</script>
  • 使用订阅消息组件:
<template>
  <view>
    <HmSubscribeMessageModal
      :isShowModal="isShowSubscribeMessageModal"
      @cancel="closeSubscribeMessageModal"
      @confirm="confirmSubscribeMessageModal"
      @not-support="isSportSubscribeMessage = false"
    />
  </view>
</template>
<script>
export default {
  data() {
    return {
      isShowSubscribeMessageModal: false,
      isSportSubscribeMessage: true, // 是否支持模板消息
    };
  },
  methods: {
    // 消息订阅确认框
    showSubscribeMessageModal() {
      this.isShowSubscribeMessageModal = true;
    },
    closeSubscribeMessageModal() {
      this.isShowSubscribeMessageModal = false;
      // 订阅失败
    },
    confirmSubscribeMessageModal() {
      this.isShowSubscribeMessageModal = false;
      // 订阅成功
    },
    submit() {
      if (this.isSportSubscribeMessage) {
        this.showSubscribeMessageModal();
      }
    },
  },
};
</script>

参考

tt.requestSubscribeMessage 返回错误

requestSubscribeMessage:fail can only be invoked by user TAP gesture 微信小程序调起订阅消息失败文章来源地址https://www.toymoban.com/news/detail-571321.html

到了这里,关于【小程序】fail can only be invoked by user TAP gesture 唤起订阅消息多端兼容解决方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • File chooser dialog can only be shown with a user activation.

    使用vue开发时,通过ref通过“this.refs.[name].$el.click()”触发按钮时提示“File chooser dialog can only be shown with a user activation.”,按钮不能触发,网上解决办法是“dispatchEvent(new MouseEvent(\\\'click\\\'))”代替“$el.click()”,在我所在的场景不生效,所以就在要出发按钮组件上加了一个id,按

    2024年01月23日
    浏览(36)
  • rabbitMQ登录报错user can only log in via localhost

    本地安装好rabbitmq,启动后,输入IP:port,打开登录页面,使用guest/guest登录,报错 这是由于guest账号默认只能通过localhost访问登录,如果通过IP地址访问,则会被限制; 解决办法: 找到rabbitmq安装位置,D:developrabbitMqrabbitmq_server-3.7.17ebin;打开文件rabbimq.app 将{loopback_users

    2024年02月17日
    浏览(27)
  • [vue warn]: inject() can only be used inside setup()

    问题背景:最近在用vue3写管理系统的登录功能的时候,在封装axios之后浏览器控制台出现警告: [Vue warn]: inject() can only be used inside setup() or functional components. 原因:因为在vue3中useRouter,useStore要放在setup中引入,我们在封装axios文件中不能直接引入。 1.bug提示:  2.然后我们就

    2024年02月05日
    浏览(38)
  • ssh 登录报 Authorized users only. All activities may be monitored and reported.

    ssh 登录报 Authorized users only. All activities may be monitored and reported. 解决: 修改 /etc/motd 文件,清空内容 修改以后,登录不报 Authorized users only. All activities may be monitored and reported. update 2023-12-04 12:35 如果还不行就干掉 以下两个文件的内容 /etc/issue /etc/issue.net 总结下最全方法: 方法

    2024年02月15日
    浏览(48)
  • Unity 解决 “... can only be called from the main thread” 问题

    有些属性或方法只能在主线程中调用,如 .gameObject 、 Instantiate() 等。这是 Unity 设计时的一个缺陷(为了保证线程安全),多年来一直没有修正,或者说官方懒得弄。 以 Instantiate() 为例,在非主线程调用时,报错大概如下所示。其他属性或方法的报错也大体相同。 注:应注意

    2024年01月17日
    浏览(48)
  • ERROR: There can be only one Game target per project.

    UATHelper: Packaging (Windows (64-bit)): ERROR: There can be only one Game target per project. D:dockIntermediateSource 把旧的文件删去 一般会出现在更改项目名称后 感谢 There can be only one Game target per project - Development Discussion / Content Creation - Unreal Engine ForumsThere can be only one Game target per project - 

    2024年02月08日
    浏览(38)
  • TypeError: only size-1 arrays can be converted to Python scalars

    Traceback (most recent call last):   File \\\"/home/yjq/socket_test/server2.py\\\", line 22, in module     msg.data = float(np.array(eval(from_client_msg.decode(\\\"gbk\\\"))))#先转换为列表,再转为数组                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: only size-1 arrays can be converted to Python scalars 这个

    2024年02月12日
    浏览(27)
  • 解决only one element tensors can be converted to Python scalars

    目录 解决 \\\"only one element tensors can be converted to Python scalars\\\" 错误 问题源头 解决方法 方法一:使用​​item()​​方法 方法二:使用索引操作 总结 语法 参数 返回值 使用场景 示例 当我们使用PyTorch进行深度学习任务时,有时会遇到以下错误信息:\\\"only one element tensors can be conve

    2024年02月03日
    浏览(34)
  • 解决TypeError: only size-1 arrays can be converted to Python scalars

    目录 解决TypeError: only size-1 arrays can be converted to Python scalars 错误示例 错误分析 解决方法 方法一:使用​​flatten()​​ 方法二:使用ravel() 结论 在Python中,当我们尝试将一个数组作为标量(scalar)进行操作时,有时会遇到 ​ ​TypeError: only size-1 arrays can be converted to Python sca

    2024年02月05日
    浏览(40)
  • TypeError: only integer scalar arrays can be converted to a scalar index

    报错信息: 类型错误,只有整型标量数组才能转换成标量索引,但一般问题都不在于你的索引是不是整数。这个报错一般会出现在你想使用一个索引列表去索引另一个列表,即诸如list[index_list]的形式,此时就会出现此报错,因为 index_list 为 List列表类型,不被允许;如果是数

    2024年02月11日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包