小程序消息推送開發(fā)記錄

開發(fā)中遇到的第一個問題

TypeError: wx.requestSubscribeMessage is not a function

經(jīng)查是基礎(chǔ)調(diào)試庫版本過低導(dǎo)致,設(shè)置最高版本可以。
小程序代碼如下:

  //消息訂閱
  sendMessage:function(e){
    wx.requestSubscribeMessage({
      tmplIds: ['訂閱消息模板id'],
      success (res) { 
        console.log(res)
      }
    })
  },

Java部分:
遇到的第一個問題是

{"errcode":47003,"errmsg":"argument invalid! data.amount10.value invalid rid: 5f44b02d-493e82c5-44ad9b73"}

查看官方文檔,表示是參數(shù)錯誤,amount10。
原來官方給的示例格式必須嚴(yán)格參照,比如純數(shù)字就必須純數(shù)字哪怕你看起來好像是可以加字符串也不可以。
第二個問題是

No mapping found for HTTP request with URI [/echar/undefined/oDI2B4phDxrNgFt1i9IBYhlAtjtc]

消息發(fā)送成功,但是點擊消息體進(jìn)入小程序的跳轉(zhuǎn)頁面時出錯,我在代碼中明明已經(jīng)設(shè)置了

 messageModel.setPage("pages/pie/pie");

但是結(jié)果是跳轉(zhuǎn)了一個完全錯誤的頁面。原因是因為我自己的小程序頁面跳轉(zhuǎn)的時候需要參數(shù),所以在你設(shè)置的頁面也需要添加參數(shù)。
排查完畢,粘貼Java代碼

package com.gaara.controller;

import com.gaara.model.WeChartModel.TemplateData;
import com.gaara.model.WeChartModel.WeChartMessageModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/********************************
 *    Author Gaara              *
 *    Version 1.0               *
 *    @ClassName SendMessageController
 *    @Date 2020/8/25 下午1:54     
 *    @Description TODO         *
 ********************************/
@RestController
public class SendMessageController {


    // 微信登陸回調(diào)地址
    @Value("${myDatas.WXLoginURL}")
    String WXLoginURL;
    // 微信APPID
    @Value("${myDatas.WXAPPID}")
    String WXAPPID;
    // 微信秘鑰
    @Value("${myDatas.SECRET}")
    String SECRET;

    /*
     * 發(fā)送訂閱消息
     * */
    @GetMapping("/pushOneUser")
    public String pushOneUser() {
        return push("目標(biāo)用戶的openid");
    }

    public String push(String openid) {
        RestTemplate restTemplate = new RestTemplate();
        //這里簡單起見我們每次都獲取最新的access_token(時間開發(fā)中,應(yīng)該在access_token快過期時再重新獲取)
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
        //拼接推送的模版
        WeChartMessageModel messageModel = new WeChartMessageModel();
        messageModel.setTouser(openid);//用戶的openid(要發(fā)送給那個用戶,通常這里應(yīng)該動態(tài)傳進(jìn)來的)
        messageModel.setTemplate_id("4nbuFFbJyN7kWTx4KdFuvDZUajLNd-FFvs0VHF2Lbso");//訂閱消息模板id
        messageModel.setPage("pages/pie/pie?type_name=month");

        Map<String, TemplateData> m = new HashMap<>(3);
        m.put("date1", new TemplateData("2020年08月22日"));
        m.put("amount10", new TemplateData("3000"));
        m.put("amount4", new TemplateData("3333"));
        m.put("thing13", new TemplateData("weedxu"));
        m.put("thing9", new TemplateData("本月超支哦"));
        messageModel.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, messageModel, String.class);
        return responseEntity.getBody();
    }


    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        params.put("APPID", WXAPPID);  //
        params.put("APPSECRET", SECRET);  //
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
        String body = responseEntity.getBody();
        JSONObject object = JSON.parseObject(body);
        String Access_Token = object.getString("access_token");
        String expires_in = object.getString("expires_in");
        System.out.println("有效時長expires_in:" + expires_in);
        return Access_Token;
    }
}

這部分代碼借鑒了另一個作者的文章(小程序訂閱消息推送),侵刪。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容