微信小程序消息推送的那些坑你遇到了几个?

今天项目小程序有个提醒用户到店取货的需求,一想这需求咱们不是手到擒来吗?意外的是还被这东西折腾了一会,大伙来看看你们有没有遇到同样问题。
1、小程序模板消息接口(已废弃),之前项目用到的估计都改了了吧各位,微信就是这么豪横!!!
2、现在采用订阅消息
3、需要用户订阅权限才能推送消息,用户订阅一次发送一次(用户未勾选默认推送每个模板消息只能发送一次,勾选能主动推送3次)
下面贴上我的案例代码供大家参考:

    public function sendMessage($orderid,$username,$code,$store_name,$store_address,$store_phone,$openid)
    {
        $wechat = new Wechat();//移入你自己的微信类
        $token = $wechat->get_access_token();//获取access_token
        $post = [];
        $post['touser'] = $openid;//被推送者的openid
        $post['page'] = '/pages/orderDetail/orderDetail?id='.$orderid;//订阅消息的跳转页面
        $post['template_id'] = '2lC4siiXRbNuj_2fEglcbNPouT3eFImvmFGs3E-GbIc';//消息模板
        $post['data'] = [
            'name1'=>['value'=>$username,'color'=>'#173177'],
            'character_string11'=>['value'=>$code,'color'=>'#173177'],
            'thing2'=>['value'=>$store_name,'color'=>'#173177'],
            'thing3'=>['value'=>$store_address,'color'=>'#173177'],
            'phone_number9'=>['value'=>$store_phone,'color'=>'#173177']
        ];//推送内容 按照你自己的模板写
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
        $re = $this->curl_url($url,$post);
        return $re;
    }

    private function curl_url($url, $json)
    {
        $body = json_encode($json);
        $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;

    }
    //.........................获取access_token............
        /**
     * Notes:获取access_token
     * User: 小海
     * Time:下午 2:55
     * DATE:2020/7/2 0002
     */
    public function get_access_token() {
        $tokenFile = "/www/wwwroot/disanxiang/application/access_token.txt";//我是将token保存在文件中,你也可以存数据库自己定就好
        $data = json_decode(file_get_contents($tokenFile));
        if ($data->expire_time < time() || !$data->expire_time) {
            //小程序appid
            $appid     = '';
            //小程序 APPSecret 密钥
            $appsecret =  '';
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
            $res = json_decode(file_get_contents($url));
            $access_token = $res->access_token;
            if($access_token) {
                $data_new['expire_time'] = time() + 7000;//有效期两小时
                $data_new['access_token'] = $access_token;
                file_put_contents($tokenFile, json_encode($data_new));
            }
        }else{
            $access_token = $data->access_token;
        }
        return $access_token;
    }

完了直接调用sendMessage方法即可,效果如下:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值