Applet ยท subscription message

1. Get template ID
Manually configure and obtain the template ID on wechat public platform:
Sign in Obtain a template. If there is no suitable template, you can apply to add a new template, which can be used after approval.

2. Obtain distribution permission
See applet side message subscription interface wx.requestSubscribeMessage for details

  // Subscription message authorization
  getMsgInfo() {
    let that = this
    // This is the place to obtain the distribution permission. According to the official document, you can obtain whether the user turns on the main switch of subscription message according to the withSubscriptions parameter of wx.getSetting(). Later, we need to obtain whether the user agrees to always agree with message push. So set it to true here.
    wx.getSetting({
      withSubscriptions: true, //  If it is set to true here, mainSwitch will be returned below
      success: function (res) {
        console.log(res)
        // Pop up the authorization interface
        if (res.subscriptionsSetting.mainSwitch) {
          // The user turned on the main switch of subscription message
          if (!res.subscriptionsSetting.itemSettings) {
            // When the user doesn't click 'always keep the above selection and don't ask' button. Then every time I hold it here, I will pull up the authorization pop-up window
            // wx.showModal({
            //   title: 'prompt',
            //   content: 'please authorize to open service notification',
            //   showCancel: true,
            //   success: function (ress) {
            //     if (ress.confirm) {
            wx.requestSubscribeMessage({
              // Call up the message subscription interface
              tmplIds: ['rSELfotEiOJ_2GtKYxRHNoKdhnnwEEPt6cqGhsqqZzQ'],
              success(res) {
                console.log('Subscription message succeeded ')
                console.log(res)
                // User agreed message template id
                let moIdState = res['rSELfotEiOJ_2GtKYxRHNoKdhnnwEEPt6cqGhsqqZzQ']
                // Object res [template_id] is a dynamic key, that is, the template ID. the values include 'accept', 'reject', 'ban' and 'filter'.
                // 'accept' means that the user agrees to subscribe to the template message corresponding to the id, 'reject' means that the user refuses to subscribe to the template message corresponding to the id, 'ban' means that it has been blocked by the background, 'filter' means that the template is filtered by the background because the template title has the same name.
                // For example, {errmsg: "requestsubscribemessage: OK", zu-lzcqyw-edafcvvzpkk4de2rllr1fpw2a_x0oxe: "accept"} indicates that the user agrees to subscribe to zu-lzcqyw-edafcvvzpkk4de2rllr1fpw2a_ X0oxe this message
                if (moIdState === 'accept') {
                  console.log('Message push accepted')
                } else if (moIdState === 'reject') {
                  console.log('Reject message push')
                } else if (moIdState === 'ban') {
                  console.log('Blocked by background')
                }
                wx.navigateTo({
                  url: '/pages/index/msg-notice/index',
                })
              },
              fail(er) {
                console.log('Failed to subscribe to message ')
                console.log(er)
                if (res.errCode === 20004) {
                  wx.showModal({
                    title: 'reminder',
                    content: 'You have refused authorization and will not be able to receive the reply notice in wechat!',
                    showCancel: false,
                    success: (res) => {
                      if (res.confirm) {
		                wx.navigateTo({
		                  url: '/pages/index/msg-notice/index',
		                })
                      }
                    },
                  })
                }
              },
            })
            //     }
            //   },
            // })
          } else {
            // The user agrees to always keep the selection of whether to push messages. This means that the authorization of pushing messages will not be pulled up in the future
            let moIdState = res.subscriptionsSetting.itemSettings['rSELfotEiOJ_2GtKYxRHNoKdhnnwEEPt6cqGhsqqZzQ'] // User agreed message template id
            if (moIdState === 'accept') {
              console.log('Message push accepted')
            } else if (moIdState === 'reject') {
              console.log('Reject message push')
            } else if (moIdState === 'ban') {
              console.log('Blocked by background')
            }
          }
        } else {
          console.log('Subscription message not turned on')
        }
      },
      fail: function (error) {
        console.log(error)
      },
    })
  },

matters needing attention
1. "Always keep the above selection and don't ask again"

If unchecked, the authorization window will pop up every time the user triggers the subscription message function.

For example, you now have two subscription messages. The first one you checked is no longer ask, the second one is not checked, and the second one will be prompted next time you click. Each subscription message is independent.

If the user checks "always keep the above selection, don't ask again" in the subscription panel, the template message will be added to the user's applet setting page. The next subscription calls wx.requestSubscribeMessage, there will be no pop-up window, and the previous selection will be kept.

The wx.getSetting interface is required to obtain the user's subscription status to relevant template messages.

If the user agrees to subscribe to this message, wx.requestSubscribeMessage will be called by default in the background, but there is no pop-up box. For example, you subscribe five times and send it five times. My test is like this.

2. Subscription messages are currently divided into "one-time subscription" and "permanent subscription"
One time subscription message
After the user subscribes once, the developer can send a message for unlimited time. Multiple templates can be.
If the user checks "always keep the above selection, don't ask again" and clicks allow, he will agree to subscribe to this message by default in the future. Users no longer make multiple choices, and developers avoid more cumbersome reminders.

Long term subscription message
After users subscribe once, developers can send multiple messages for a long time.
At present, long-term subscription news is only open to offline public services such as government affairs and people's livelihood, medical treatment, transportation, finance and education, and will gradually support other offline public service businesses in the later stage.
(long term subscription messages are only available for specific industries, so ordinary developers cannot use them)


reference resources:
Applet subscription message
Applet side message subscription interface wx.requestSubscribeMessage
Applet template message capability adjustment notification
Wechat applet subscription message module for novices who are prone to misunderstandings
Wechat applet subscription message authorization selects to always receive subscription messages. After successfully sending the subscription message once, the subsequent sending will return 43101 error?

Keywords: Mini Program wechat

Added by didgydont on Wed, 13 Oct 2021 03:12:00 +0300