Current Location: Home> Latest Articles> Detailed Guide to Implementing WeChat Mini Program Push Notifications with PHP

Detailed Guide to Implementing WeChat Mini Program Push Notifications with PHP

M66 2025-07-31

How to Implement Push Notifications for WeChat Mini Programs Using PHP?

With the widespread adoption of WeChat Mini Programs, developers often need to send push notifications to users to deliver important updates or event reminders promptly. This article introduces the key steps to implement push notifications in WeChat Mini Programs using PHP and provides specific code examples to help developers get started quickly.

Preparation

Before starting, developers need to prepare the following essential information:

  • WeChat Mini Program's AppID and AppSecret, which can be obtained from the WeChat public platform backend for authentication purposes.
  • User's access_token, used to call WeChat's push notification API, typically acquired via the Mini Program login interface.

Obtaining the access_token

Before sending push notifications, the access_token must be obtained. The PHP function below demonstrates how to request the access_token using AppID and AppSecret:

function getAccessToken($appid, $appsecret) {
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
    $result = file_get_contents($url);
    $result = json_decode($result, true);
    return $result['access_token'];
}

// Example usage
$appid = 'your_appid';
$appsecret = 'your_appsecret';
$access_token = getAccessToken($appid, $appsecret);

Sending Push Notifications

Once the access_token is obtained, you can use the official WeChat API to send subscription messages. Below is a PHP example for sending a push notification:

function sendNotification($access_token, $openid, $title, $content, $page = '') {
    $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
    $data = array(
        'touser' => $openid,
        'template_id' => 'your_template_id',
        'page' => $page,
        'data' => array(
            'thing1' => array('value' => $title),
            'thing2' => array('value' => $content),
        ),
    );
    $data = json_encode($data);
    $options = array(
        'http' => array(
            'header'  => "Content-type:application/json",
            'method'  => 'POST',
            'content' => $data,
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    $result = json_decode($result, true);
    return $result['errmsg'] == 'ok';
}

// Example usage
$openid = 'your_openid';
$title = 'This is a push notification title';
$content = 'This is the content of the push notification';
$page = 'pages/index/index';  // Optional, redirects to a specific page
$result = sendNotification($access_token, $openid, $title, $content, $page);
if ($result) {
    echo "Push notification sent successfully!";
} else {
    echo "Failed to send push notification.";
}

Important Notes

  • your_template_id is the ID of a custom message template created in the WeChat Mini Program backend and must be obtained beforehand.
  • The data array's thing1 and thing2 correspond to variables defined in the template and can be adjusted as needed.
  • The page parameter is optional; if provided, the user clicking the message will be directed to the specified page; otherwise, it defaults to the home page.

Summary

This article outlines the complete process of implementing push notifications in WeChat Mini Programs using PHP, covering both obtaining the access_token and sending messages. With the official WeChat API and the provided code examples, developers can quickly integrate push notification features to enhance user engagement.