微信消息 长期消息订阅 通知

微信消息一次性消息订阅与长期消息订阅开启用户提醒

1、首先需要再微信公众号平台开启 消息订阅
个人用户只能订阅一次性消息
类型是公共服务可以订阅长期消息订阅

在这里插入图片描述
2.微信小程序代码

const tmplIds = ['模板id1','模板id2','模板id3']

//开启消息订阅
function openMessageNotfiy() {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log('消息订阅设置结果', res)
        // 1.如果用户未开启消息订阅的通知管理
        if (!res.subscriptionsSetting.mainSwitch) {
          wx.showModal({
            title: '温馨提示',
            content: "需要打开接收通知并设置每个提醒为'接受',请点击确定进行授权",
            success(modalRes) {
              if (modalRes.confirm) {
                wx.openSetting()
              }
            }
          });
          return;
        } else {
          requestSubscription(res)
        }
      }
    })
  })
}

// 请求消息订阅提醒
function requestSubscription(result) {
  wx.requestSubscribeMessage({
    tmplIds,
    success(res) {
      const subscriptionsSetting = result.subscriptionsSetting
      if (subscriptionsSetting[tmplIds[0]] !== "reject" && subscriptionsSetting[tmplIds[1]] !== "reject" && subscriptionsSetting[tmplIds[2]] !== "reject") {
        // 用户同意了订阅
        console.log('订阅成功');
      } else {
        // 用户拒绝了订阅或发生了其他异常
        wx.showModal({
          title: '温馨提示',
          content: "需要打开接收通知并设置每个提醒为'接受',请点击确定进行授权",
          success(modalRes) {
            if (modalRes.confirm) {
              wx.openSetting()
            }
          }
        });
        console.log('订阅失败');
      }
    },
    fail(err) {
      console.error('请求订阅失败', err);
    }
  });
}

需要用户点击操作才可以调用 openMessageNotfiy()

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一头小绵羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值