国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【微信公眾號推送小程序消息通知】--下發(fā)統(tǒng)一消息接口被回收后新方案

這篇具有很好參考價值的文章主要介紹了【微信公眾號推送小程序消息通知】--下發(fā)統(tǒng)一消息接口被回收后新方案。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

序言

由于微信小程序長期訂閱的消息模板和下發(fā)統(tǒng)一消息推送接口全部失效以后,對于小程序的消息推送可以往公眾號推可以使用本文章方案。在網(wǎng)上看了挺多方案,有用用戶列表做匹配的等,最終覺得通過關(guān)注事件觸發(fā)的方案是最省事。

準(zhǔn)備

1、微信公眾平臺注冊服務(wù)號(訂閱號是不可以推送的)與小程序,兩者都需要認證并且認證主體是一致

2、微信開放平臺注冊賬號(該賬號也需要認證),綁定小程序與公眾號
微信消息推送接口,微信,小程序,spring boot,微信公眾平臺
3、公眾號根據(jù)想要的模板消息綁定服務(wù)類目,去模板消息中先挑選你的模板消息。(如果是剛注冊的公眾號還需要去新的功能頁面添加模板消息功能,需要微信審核,不過很快)
微信消息推送接口,微信,小程序,spring boot,微信公眾平臺
4、微信公眾號綁定小程序
微信消息推送接口,微信,小程序,spring boot,微信公眾平臺
5、小程序與公眾號配置服務(wù)器的ip地址白名單
微信消息推送接口,微信,小程序,spring boot,微信公眾平臺
6、公眾號配置服務(wù)器地址
微信消息推送接口,微信,小程序,spring boot,微信公眾平臺

整體實現(xiàn)流程

通過在開放平臺綁定的公眾號與小程序后,我們在調(diào)用微信code2Session接口的時候會返回unionid,這個unionid就是推送的關(guān)鍵。

  • 用到微信接口:
    1、關(guān)注事件推送:用關(guān)注事件推送接口文檔地址
    2、查詢用戶基礎(chǔ)信息:用戶基礎(chǔ)信息接口文檔地址
    3、獲取accessToken:獲取accessToken接口文檔地址
    4、消息模板推送:消息模板推送接口文檔地址
    微信消息推送接口,微信,小程序,spring boot,微信公眾平臺

實現(xiàn)代碼(Java)

這里使用第三方工具包Wx-Java(非常方便),直接實現(xiàn)推送代碼。具體源碼可以瀏覽https://gitee.com/binary/weixin-java-tools文章來源地址http://www.zghlxwxcb.cn/news/detail-763903.html

  • 先在配置文件yml配置公眾號信息
# 公眾號配置
wx:
  mp:
    appId: xiaochengxuappid
    secret: xiaochengxusecrect
    token: xiaochengxutoken
    aesKey:  xiaochengxuaes
    # token存儲在redis
    config-storage:
      type: RedisTemplate
  • 封裝推送工具類
	/**
     * 微信公眾號推送模板消息
     *
     * @param openid       用戶openid
     * @param templateData 模板參數(shù)
     */
    public void sendTemplateMsg(String openid, List<WxMpTemplateData> templateData) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)
                // 模板id
                .templateId(templateId)
                // 跳轉(zhuǎn)小程序appid,跳轉(zhuǎn)路徑
                .miniProgram(new WxMpTemplateMessage.MiniProgram(weAppAppId, "", false))
                // 模板參數(shù)
                .data(templateData)
                .build();
        try {
            log.debug("微信公眾號推送模板消息入?yún)ⅲ簕}", templateMessage);
            // 成功返回msgId,否則都是拋異常
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            log.error("微信公眾號推送模板消息異常", e);
        }
    }
  • 公眾號接入服務(wù)器開發(fā)的代碼
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author quan
 * @date 2023/11/7 18:19
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/wx")
public class WxPortalController {
    @Autowired
    private final WxMpService wxService;
    @Autowired
    private final WxMpMessageRouter messageRouter;

    @GetMapping(produces = "text/plain;charset=utf-8")
    public String authGet(@PathVariable String appid,
                          @RequestParam(name = "signature", required = false) String signature,
                          @RequestParam(name = "timestamp", required = false) String timestamp,
                          @RequestParam(name = "nonce", required = false) String nonce,
                          @RequestParam(name = "echostr", required = false) String echostr) {
        log.info("接收到來自微信服務(wù)器的認證消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
        if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
            throw new IllegalArgumentException("請求參數(shù)非法,請核實!");
        }

        if (!this.wxService.switchover(appid)) {
            throw new IllegalArgumentException(String.format("未找到對應(yīng)appid=[%s]的配置,請核實!", appid));
        }

        if (wxService.checkSignature(timestamp, nonce, signature)) {
            return echostr;
        }

        return "非法請求";
    }

    @PostMapping(produces = "application/xml; charset=UTF-8")
    public String post(@PathVariable String appid,
                       @RequestBody String requestBody,
                       @RequestParam("signature") String signature,
                       @RequestParam("timestamp") String timestamp,
                       @RequestParam("nonce") String nonce,
                       @RequestParam("openid") String openid,
                       @RequestParam(name = "encrypt_type", required = false) String encType,
                       @RequestParam(name = "msg_signature", required = false) String msgSignature) {
        log.info("接收微信請求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
                        + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
                openid, signature, encType, msgSignature, timestamp, nonce, requestBody);

        if (!this.wxService.switchover(appid)) {
            throw new IllegalArgumentException(String.format("未找到對應(yīng)appid=[%s]的配置,請核實!", appid));
        }

        if (!wxService.checkSignature(timestamp, nonce, signature)) {
            throw new IllegalArgumentException("非法請求,可能屬于偽造的請求!");
        }

        String out = null;
        if (encType == null) {
            // 明文傳輸?shù)南?/span>
            WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
            WxMpXmlOutMessage outMessage = this.route(inMessage);
            if (outMessage == null) {
                return "";
            }

            out = outMessage.toXml();
        } else if ("aes".equalsIgnoreCase(encType)) {
            // aes加密的消息
            WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxService.getWxMpConfigStorage(),
                    timestamp, nonce, msgSignature);
            log.debug("消息解密后內(nèi)容為:{} ", inMessage);
            WxMpXmlOutMessage outMessage = this.route(inMessage);
            if (outMessage == null) {
                return "";
            }

            out = outMessage.toEncryptedXml(wxService.getWxMpConfigStorage());
        }

        log.debug("組裝回復(fù)信息:{}", out);
        return out;
    }

    private WxMpXmlOutMessage route(WxMpXmlMessage message) {
        try {
            return this.messageRouter.route(message);
        } catch (Exception e) {
            log.error("路由消息時出現(xiàn)異常!", e);
        }

        return null;
    }
}

  • 微信消息路由配置類
/**
 * @author quan
 * @date 2023/11/7 18:23
 */
@AllArgsConstructor
@Configuration
public class WxMpConfiguration {
    private final SubscribeHandler subscribeHandler;

    @Bean
    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

        // 關(guān)注事件
        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();

        return newRouter;
    }
}
  • 微信關(guān)注事件處理類
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tfcs.web.domain.bus.BusWxMpUser;
import com.tfcs.web.service.BusWxMpUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 關(guān)注處理器
 *
 * @author quan
 * @date 2023/9/15 14:05
 */
@Slf4j
@Component
public class SubscribeHandler implements WxMpMessageHandler {
    @Autowired
    private BusWxMpUserService wxMpUserService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        log.info("新關(guān)注用戶 OPENID: " + wxMessage.getFromUser());

        // 獲取微信用戶基本信息
        try {
            WxMpUser userWxInfo = weixinService.getUserService()
                    .userInfo(wxMessage.getFromUser(), null);
            if (userWxInfo != null) {
                // TODO 先檢查是否存在該用戶,不存在再存到數(shù)據(jù)庫
            }
        } catch (WxErrorException e) {
            log.error("微信公眾號獲取用戶信息異常:", e);
            if (e.getError().getErrorCode() == 48001) {
                log.info("該公眾號沒有獲取用戶信息權(quán)限!");
            }
        }
        return null;
    }
}
  • 業(yè)務(wù)代碼調(diào)用demo
// 通過unionid查出公眾號openid
BusWxMpUser wxMpUser = wxMpUserService.getOne(new LambdaQueryWrapper<BusWxMpUser>()
         .eq(BusWxMpUser::getUnionid, user.getUnionid()));
 if (null != wxMpUser) {
     List<WxMpTemplateData> templateData = new ArrayList<>(5);
     // 對應(yīng)消息模板的key
     templateData.add(new WxMpTemplateData("character_string2", event.getFlightNo()));
     templateData.add(new WxMpTemplateData("thing10", event.getStartPlace()));
     templateData.add(new WxMpTemplateData("thing11", event.getDestPlace()));
     templateData.add(new WxMpTemplateData("time3", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, event.getFlightStartTime())));
     templateData.add(new WxMpTemplateData("thing9", "請及時處理關(guān)注事件"));
     weChatUtil.sendTemplateMsg(wxMpUser.getOpenid(), templateData);
 }
  • 推送效果
    微信消息推送接口,微信,小程序,spring boot,微信公眾平臺

到了這里,關(guān)于【微信公眾號推送小程序消息通知】--下發(fā)統(tǒng)一消息接口被回收后新方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 微信小程序向公眾號推送消息模板

    微信小程序向公眾號推送消息模板

    由于微信小程序長期訂閱的消息模板全部失效以后,對于小程序的消息推送可以改成往公眾號推。 這里將介紹如何使用小程序向公眾號推送消息,并且消息可以跳轉(zhuǎn)到小程序 1、微信公眾平臺注冊 服務(wù)號 (訂閱號是不可以推送的)與小程序,兩者都需要認證并且 認證主體是

    2024年02月06日
    瀏覽(50)
  • 微信小程序服務(wù)通知(訂閱消息)定時推送消息功能

    微信小程序服務(wù)通知(訂閱消息)定時推送消息功能

    首先先說項目需求:向預(yù)約參觀的用戶提前一天晚上8點推送消息。小程序端主要用到的 API 是我是小程序用到的API。以及服務(wù)端用到的 API :我是服務(wù)端用到的API。 1. 開通訂閱消息功能 (1)、 首先需要在小程序管理后臺開通訂閱消息功能。沒開通前如下圖所示: (2)、開通之

    2024年02月08日
    瀏覽(17)
  • 微信小程序向公眾號推送消息超詳細教程

    微信小程序向公眾號推送消息超詳細教程

    官方教程 官方教程 開通一下服務(wù)號公眾號 超級管理員登錄服務(wù)號公眾號后臺 登錄地址 開通模板消息 申請一個模板消息,獲取模板ID 注意此處的參數(shù),后續(xù)接口需要使用 綁定公眾號與小程序 官方教程 1.登錄微信公眾號后臺 2.點擊小程序管理 3.關(guān)聯(lián)小程序 獲取微信公眾號

    2024年02月11日
    瀏覽(24)
  • 如何為微信小程序添加訂閱消息和推送通知功能

    為微信小程序添加訂閱消息和推送通知功能是非常有用的,它可以讓用戶在重要的事件發(fā)生時及時地收到通知。在本文中,我們將詳細介紹如何為微信小程序添加這兩種功能。 一、訂閱消息 訂閱消息是一種新的消息類型,用戶可以選擇是否訂閱它們。訂閱消息一般用于向用

    2024年02月04日
    瀏覽(35)
  • spring boot +微信小程序項目,通過微信公眾號實現(xiàn)指定用戶消息長期推送

    spring boot +微信小程序項目,通過微信公眾號實現(xiàn)指定用戶消息長期推送

    用戶登錄小程序,后臺記錄用戶的小程序openId和用戶唯一的UnionId。然后用戶觸發(fā)公眾號事件(關(guān)注公眾號或者發(fā)送指定消息),后臺獲取到用戶公眾號的openId,再調(diào)用接口通過公眾號的openId查詢用戶的UnionId,再和數(shù)據(jù)庫里的UnionId進行匹配,將用戶的公眾號openId存入數(shù)據(jù)庫。此

    2024年02月03日
    瀏覽(57)
  • uni-app中實現(xiàn)微信小程序/公眾號訂閱消息推送功能

    uni-app中實現(xiàn)微信小程序/公眾號訂閱消息推送功能

    ??????? 熱愛攝影的程序員 ??????? 喜歡編碼的設(shè)計師 ???? 擅長設(shè)計的剪輯師 ??????? 一位高冷無情的編碼愛好者 大家好,我是全棧 IT 工程師摘星人 歡迎分享 / 收藏 / 贊 / 在看! 開發(fā)業(yè)務(wù)時時常遇到需要向用戶發(fā)送一些通知,如欠費通知、會員到期通知等等。

    2024年02月02日
    瀏覽(97)
  • 微信小程序統(tǒng)一分享,全局接管頁面分享消息的一些技巧

    前言 最近都在折騰自己的個人內(nèi)容聚合小程序。除了作為原創(chuàng)專欄,視頻教程的聚合。我有什么新的想法,產(chǎn)品創(chuàng)意,最終落地的東西都會放到這個小程序里。 而分享功能非常的重要,當(dāng)某一個功能或文章打動用戶的時候,能把這個小程序分享出去,就能帶來裂變傳播的效

    2024年02月15日
    瀏覽(25)
  • 微信公眾號推送模板消息給用戶

    微信公眾號推送模板消息給用戶

    前置條件: 1.公眾號為服務(wù)號,而非訂閱號 2.認證(300元) 3.進入公眾號申請模板推送功能 4.添加模板(注意:推送的消息只能使用微信提供的模板,不可自定義,但也是比較全的) 4.2 獲取accessToken時,需要將開發(fā)環(huán)境的電腦ip添加到微信后臺的ip白名單(線上環(huán)境亦是如此

    2024年02月12日
    瀏覽(17)
  • java 實現(xiàn)微信公眾號消息推送

    java 實現(xiàn)微信公眾號消息推送

    這里主要用測試賬號來演示:測試賬號注冊地址 正式賬號注冊地址:微信公眾平臺 可參考微信公眾號文檔:微信公眾號文檔 模板根據(jù)需求設(shè)置(注意:參數(shù)長度不能超出微信規(guī)定,否則將發(fā)送失敗) 參數(shù)要求規(guī)則 依賴 請求地址、appid、密鑰等信息;信息配置到application配置中

    2024年02月06日
    瀏覽(46)
  • 小程序推送公眾號模板消息

    小程序推送公眾號模板消息

    第一步:先創(chuàng)建微信開放平臺:微信開放平臺,綁定微信小程序和公眾號(是為后面拿共用的unionid進行小程序openid與公眾號openid綁定) 第二步:進入公眾號開放平臺,找到基本配置,配置服務(wù)器地址(url):指自己服務(wù)器能夠訪問的域名 第三步:在服務(wù)器中就是url寫下這個方法

    2024年01月21日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包