WeChat applet subscription message process

Foreword:

Every time I subscribe to a message, I paste and copy it online. Sometimes it works, sometimes it doesn't work, and it may not be able to be aroused. I encountered a lot of strange phenomena, so I took some time to sort it out, for reference only.

Features/Process:

【method】

wx.getSetting,

withSubscriptions: true, 

res.subscriptionsSetting.mainSwitch,

res.subscriptionsSetting.itemSettings,

wx.showModal,

wx.openSetting,

wx.requestSubscribeMessage

【Purpose】

Get authorization status

【process】

State 1: The main switch is on - template messages are authorized - all template messages are authorized (one of the above methods will pass, so it is judged by traversing the template id)

State 2: The main switch is on - authorized template messages - there are unauthorized template messages (one of the above methods will pass, so it is judged by traversing the template id)

State 3 Main switch is on - Unauthorized template message

State 4: The main switch is closed

Status 1_1 -> Next step (e.g. submit order)

Status 2_1 -> Prompt secondary confirmation pop-up window (it is detected that a subscription message has not been opened, do you want to go to settings?)

Status 3_1 -> Evoke authorization pop-up window (wx.requestSubscribeMessage)

Status 4_1 -> Prompt secondary confirmation pop-up window (it is detected that you have not turned on the subscription message, do you want to go to settings?)

Status 2_1_1 -> Cancel Go to -> Next step (e.g. submit order)

Status 2_1_2 -> OK -> Jump to the setting page (openSetting)

Status 3_1_1 -> Uncheck the template. If not checked, the above selection will always be maintained and can only be cancelled.

Status 3_1_2 -> Uncheck the template, check it to always keep the above selection, and can only cancel it

Status 3_1_3 -> Check a certain template in the template. If not checked, the above selection will always be maintained. Cancel

Status 3_1_4 -> Check a certain template in the template. If not checked, the above selection will always be maintained. Allow

Status 3_1_5 -> Check a certain template in the template, check Always keep the above selection, Cancel

Status 3_1_6 -> Check a certain template in the template, check Always keep the above selection, allow

Status 3_1_7 -> Check all templates, uncheck to always keep the above selection, cancel

Status 3_1_8 -> Check all templates, uncheck always keep the above selection, allow

Status 3_1_9 -> Check all templates, check Always keep the above selection, Cancel

Status 3_1_10 -> Check all templates, check Always keep the above selection, allow

Status 4_1_1 -> Cancel Go to -> Next step (e.g. submit order)

Status 4_1_2 -> OK -> Jump to the setting page (openSetting)

Status 2_1_2_1 -> unchecked/unchecked -> after return -> next step (e.g. submit order)

Status 2_1_2_2 -> Checked -> After return -> Next step (e.g. submit order)

Status 3_1_1_1 -> The template will not appear in the settings after cancellation -> Next step (such as submitting an order)

Status 3_1_2_1 -> After cancellation, templates will appear in the settings, all of which are not accepted -> Next step (such as submitting an order)

Status 3_1_3_1 -> The template will not appear in the settings after cancellation -> Next step (such as submitting an order)

Status 3_1_4_1 -> After permission, the template will not appear in the settings (whether the permission is effective at this time remains to be discussed) -> Next step (such as submitting an order)

Status 3_1_5_1 -> After cancellation, templates will appear in the settings, all of which are not accepted -> Next step (such as submitting an order)

Status 3_1_6_1 -> After permission, the template will appear in the settings. The checked one is the receiving status, and the unchecked one is the non-receiving status -> Next step (such as submitting an order)

Status 3_1_7_1 -> The template will not appear in the settings after cancellation -> Next step (such as submitting an order)

Status 3_1_8_1 -> After permission, the template will not appear in the settings -> Next step (such as submitting an order)

Status 3_1_9_1 -> After cancellation, templates will appear in the settings, all of which are not accepted -> Next step (such as submitting an order)

Status 3_1_10_1 -> After permission, the template will appear in the settings, all are in the receiving status -> Next step (such as submitting an order)

Status 4_1_2_1 -> Turn on the main switch for receiving subscription messages

Status 4_1_2_2 -> Disable the main switch for receiving subscription messages

Status 4_1_2_1_1 -> After returning -> Prompt a second confirmation pop-up window (it is detected that a subscription message has not been opened, do you want to go to settings?)

Status 4_1_2_2_1 -> After return -> Next step (e.g. submit order)

Status 4_1_2_1_1_1 -> Cancel -> Next step (e.g. submit order)

Status 4_1_2_1_1_2 -> OK -> Status 3_1

Summarize

If the authorization pop-up window is unchecked, always keep the above selection button. No matter whether you select or not select a template, cancel or allow, the template will not appear in the settings (whether the template is selected and allowed is successful? Unknown)

Always keep the above selection button in the authorization pop-up window. After canceling, no matter you select all templates or a certain one, the template will appear in the settings and will be not received.

Always keep the above selection checked in the authorization pop-up window. After allowing, the template will appear in the settings and the status will be checked.

After numerous operations, setting failures and authorization failures were confirmed again, and the situation was basically closed.

Notice

If after setting the main switch, returning to the detection template without authorization cannot directly evoke the authorization pop-up window, you need to use the secondary confirmation pop-up window to confirm it.

Process screenshot

 

main switch

 

 

The template is not opened/the Always Keep button is not checked and cancelled.

 

Enable all templates/check Always keep and allow

 

Template part is turned on/check Always keep allowed

 

 

 

Authorization pop-up window

Complete code

The methods are written separately for ease of understanding. The disadvantage is that there are repetitive codes. Please encapsulate and merge them yourself.

The first step is thia.dingyue();

that.onSubmit(); is the next submission method

// 验证订阅状态
  dingyue(type){
    let that = this;
    wx.getSetting({
      withSubscriptions: true,
      success: function(res) {
        if (res.subscriptionsSetting.mainSwitch) {
          // 用户已开启总开关
          let itemSettings = res.subscriptionsSetting.itemSettings;
          if (itemSettings) {
            // 用户已授权模板消息
            let status = true; 
            //判断模板是否全部授权
            for(let i=0; i<tmplIds.length; i++){
              let key = tmplIds[i];
              if(itemSettings[key] != 'accept'){
                status = false;
                break;
              }
            }
            if(!status){
              that.weidingyue_muban();
            }else{
              that.onSubmit();
            }
          } else {
            // 用户未授权任何模板消息
            if(type == 'kaiguan'){ // 设置返回后提示是否授权模板
              that.onSettingNotSubscribeMessage();
              return;
            }
            that.openSubscribeMessage();
          }
        } else {
          // 用户未开启总开关
          if(type == 'kaiguan'){ // 设置返回后仍未开启进入下一步
            that.onSubmit();
            return;
          }
          that.weidingyue_kaiguan();
        }
      }
    });
  },

  // 未开启订阅 开关
  weidingyue_kaiguan(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '检测到您未开启订阅消息,是否前往设置?',
      success (res) {
        if (res.confirm) {
          that.openSetting('kaiguan');
        } else if (res.cancel) {
          that.onSubmit();
        }
      }
    })
  },

  // 未开启订阅 模板
  weidingyue_muban(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '检测到有订阅消息未开启,是否前往设置?',
      success (res) {
        if (res.confirm) {
          that.openSetting('muban');
        } else if (res.cancel) {
          that.onSubmit();
        }
      }
    })
  },

  // 打开设置
  openSetting(type){
    let that = this;
    wx.openSetting({
      success: function(res) {
        // 返回后流程
        if(type == 'muban'){
          that.onSubmit();
        } else if(type == 'kaiguan'){
          // 再次验证订阅状态
          that.dingyue('kaiguan');
        }
      },
      fail: function(err) {
        that.settingFail(type);
      }
    });
  },

  // 设置失败
  settingFail(type){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '设置失败,是否重新设置?',
      success (res) {
        if (res.confirm) {
          that.openSetting(type);
        } else if (res.cancel) {
          that.onSubmit();
        }
      }
    })
  },

  // 设置开启总开关返回后,并未授权的情况(如果不加二次确认弹窗,无法直接唤起授权弹窗)
  onSettingNotSubscribeMessage(){
    let that = this;
    wx.showModal({
      title: '提示',
      content:'检测到有订阅消息未授权,是否授权?',
      success (res) {
        if (res.confirm) {
          that.openSubscribeMessage();
        } else if (res.cancel) {
          that.onSubmit();
        }
      }
    })
  },

  // 唤起授权订阅弹窗
  openSubscribeMessage(){
    let that = this;
    wx.requestSubscribeMessage({
      tmplIds,
      success(res) {
        that.onSubmit();
      },
      fail(err) {
        that.subscribeMessageFail();
      }
    })
  },

  // 授权失败
  subscribeMessageFail(){
    let that = this;
    wx.showModal({
      title: '提示',
      content:'授权失败,是否重新授权?',
      success (res) {
        if (res.confirm) {
          that.openSubscribeMessage();
        } else if (res.cancel) {
          that.onSubmit();
        }
      }
    })
  },

 

Guess you like

Origin blog.csdn.net/m0_61087678/article/details/132173717