PHP code example of larva / laravel-wechat-notification-channel

1. Go to this page and download the library: Download larva/laravel-wechat-notification-channel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

larva / laravel-wechat-notification-channel example snippets


namespace App\Models;

class User {
    public function routeNotificationForWechat(){
        return $this->wechatid;
    }
    
    public function routeNotificationForWechatMiniProgram(){
        return $this->wechatminiid;
    }

    public function routeNotificationForWechatSubscribe(){
        return $this->wechatminiid;
        // or
        // $notifiable->routeNotificationFor('wechatMiniProgram',$this) 通过小程序获取 不再设置此方法
    }
}

namespace App\Notifications;

use Illuminate\Notifications\Notification;

class WelcomeNotification extends Notification
{
    /**
     * Get the notification's channels.
     *
     * @param mixed $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['wechat','wechatMiniProgram'];
    }

    /**
     * Build the wechat representation of the notification.
     *
     * @param mixed $notifiable
     * @return array|false
     */
    public function toWechat($notifiable)
    {
        if (!$toUser = $notifiable->routeNotificationFor('wechat',$this)) {
            return false;
        }
        return [
            'touser' => $toUser,
            'template_id' => 'template-id',
            'page' => 'index',
            'form_id' => 'form-id',
            'data' => [
               'keyword1' => 'VALUE',
               'keyword2' => 'VALUE2',
                    // ...
            ],
        ];
    }
    
    /**
     * Build the MiniProgram representation of the notification.
     *
     * @param mixed $notifiable
     * @return array|false
     */
    public function toWechatMiniProgram($notifiable)
    {
        if (!$toUser = $notifiable->routeNotificationFor('wechatMiniProgram',$this)) {
            return false;
        }
        return [
            'touser' => $toUser,
            'template_id' => 'template-id',
            'page' => 'index',
            'form_id' => 'form-id',
            'data' => [
               'keyword1' => 'VALUE',
               'keyword2' => 'VALUE2',
                   // ...
            ],
        ];
    }

    /**
     * Build the WechatSubscribeChannel representation of the notification.
     *
     * @param mixed $notifiable
     * @return array|false
     */
    public function toWechatSubscribe($notifiable)
    {
        if (!$toUser = $notifiable->routeNotificationFor('wechatSubscribe',$this)) {
            return false;
        }
        return [
            'touser' => $toUser,
            'template_id' => 'template-id',
            'page' => 'pages/index/index',
            'miniprogram_state' => env('APP_DEBUG', false) ? 'trial' : 'formal',
            // 跳转小程序类型:developer 为开发版;trial为体验版;formal为正式版;默认为正式版
            'data' => [
                'name1' => 'name1',
                'thing2' => 'thing2',
                //...
            ],

        ];
    }
}