Small program notification message push

Recently, the company is working on WeChat mini-programs: lottery mini-programs, check-in mini-programs; as the person in charge of the project, I track the progress at any time, and the front-end of the mini-program participated in writing a little 2 or 3 simple pages, and I built and developed the back-end system. Come and wait, I feel that the front-end pages of the WeChat applet really want vue, tags and bindings, etc. The front-end of the applet will write the next article. This article mainly introduces the implementation of the notification message push of the applet;

You can go to the official documentation of the applet:

https://developers.weixin.qq.com/miniprogram/dev/api/notice.html#%E6%A8%A1%E7%89%88%E6%B6%88%E6%81%AF%E7%AE%A1%E7%90%86

Let's look at the effect first:


 

This is the relevant configuration applied in the WeChat applet


 Application result:

 



 This is the parameter that needs to be filled in the application



 

This is an example of a message template provided by the documentation

 , so let's see how to implement it:

Step 1: Configure parameters, such as APPID, receive user openID, appsec, template ID templateid (apply in WeChat), url (click the message to transfer to the page of the applet), form_id (the front end is different each time, WeChat prevents spamming The message is sent by default when the user clicks), etc.;

Step 2: Get getAccessToken;

Step 3: Request the WeChat message sending interface;

It's very simple. I used it for 3 hours to apply for a template ID in the background of the official account, and I got it through.

 

Below is the code;

<?php

class IndexAction {

    private $appid;

    private $appsec;

    private $templateid;

    private $url;

    function __construct(){

        

        $this->appid = 'wxcxxxxxxxxxxxxxxxxxxxxxxxxx;

        $this->appsec = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

        

        $this->templateid = 'xxxxxxxxxxxxxxxxxxxxxx'; //Wechat public account background application

        $this->url = '/pages/award-detail/award-detail?id=31'; //Jump address

    }

 

    public function index()

    {

        $access_token = S("token"); //WeChat says 7200 is valid, add a cache to avoid frequent requests

        if(!$token){

            $access_token = $this->getAccessToken();

            S("token",$access_token,7200);

        }

      

        $data=[

            'touser'=> 'ocsi84gKLeAQj_jgNfjXxQRJnUHYKL', //Chen Haibo openid

            'template_id'=> $this->templateid,

            'page'=> $this->url,

            'form_id'=> 'e235b9dfbd7b156ee16b5347e912e3f6j9k7', //The front end is different for each click

            'topcolor'=>"#FF0000",

            'data'=>array(

                'keyword1'=>array('value'=>'键盘',"color"=>"#173177"),

                'keyword2'=>array('value'=>'Keyword Sampling Notice of Shanghai xx Sampling Inspection',"color"=>"#173177")

            )

        ];

        //http request wechat sending interface

        $result = $this->curl_post_send_information($access_token,json_encode($data));

        

        dump($result);die;

    }

    

    

    // Get access_token

    private function getAccessToken(){

        

        $appid = $this->appid;

        $appsec = $this->appsec;

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsec}";

        //$raw = curl_get($url);

        $raw = $this->curl_get_https($url);

        if(strlen($raw)>0){

            $data = json_decode($raw,true);

            if(json_last_error()==JSON_ERROR_NONE){

                if(key_exists('access_token',$data)){

                    return $data['access_token'];

                }else{

                    return false;

                }

            }else{

                return false;

            }

        }else{

            return false;

        }

    }

 

    //curl get session

    private function curl_get_https($url){

        $curl = curl_init(); // start a CURL session

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_HEADER, 0);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // skip certificate check

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // Check whether the SSL encryption algorithm exists from the certificate

        $tmpInfo = curl_exec($curl); //returns the json object of the api

        //close the URL request

        curl_close($curl);

        return $tmpInfo; //return json object

    }

    

    private function curl_post_send_information( $token,$vars,$second=120,$aHeader=array())  

    {  

        $ch = curl_init();  

        //overtime time  

        curl_setopt($ch,CURLOPT_TIMEOUT,$second);  

        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);  

        //Set the proxy here, if any  

        curl_setopt($ch,CURLOPT_URL,'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$token);  

        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);  

        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

        if( count($aHeader) >= 1 ){  

            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);  

        }

        curl_setopt($ch,CURLOPT_POST, 1);  

        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);  

        $data = curl_exec($ch);  

        if($data){  

            curl_close($ch);  

            return $data;  

        }  

        else {  

            $error = curl_errno($ch);  

            curl_close($ch);  

            return $error;  

        }  

    }  

}

One of the more annoying problems is that you can only send one message, and multiple users need to add a foreach outside, which is a pain in the ass. The Aurora push of the APP before is better (single, group, and all notifications). ), you can go to the WeChat public account API to see, there are still many useful interfaces and documents to obtain system information, user information, etc.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326108814&siteId=291194637