PHP code example of al-one / laravel-xiaomi-push

1. Go to this page and download the library: Download al-one/laravel-xiaomi-push 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/ */

    

al-one / laravel-xiaomi-push example snippets


# optional if >= 5.5
# config/app.php


return [

    'providers' => [
        Alone\LaravelXiaomiPush\ServiceProvider::class,
    ],

];

# config/services.php
[
    'xiaomi_push' => [
        'send_by' => 'account',
        'android' => [
            'bundle_id' => 'com.app.bundle_id',
            'appid'     => '1234567890123456',
            'key'       => '1234567890123456',
            'secret'    => 'abcdefghijklmn==',
        ],
        'ios' => [
            'bundle_id' => 'com.app.bundle_id',
            'appid'     => '1234567890123456',
            'key'       => '1234567890123456',
            'secret'    => 'abcdefghijklmn==',
            'sandbox'   => false,
        ],
    ],
];



namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model
{

    use Notifiable;

    /**
     * 小米推送路由
     */
    public function routeNotificationForXiaomiPush()
    {
        // return $this->xiaomi_push_regid;
        return $this->getKey();
    }
    
    /**
     * 如果不同用户所属的APP包名可能不同,请添加此方法
     */
    public function getAppPackage()
    {
        return 'com.app.bundle_id';
    }
    
    /**
     * 添加此方法以判断用户是否为苹果设备用户
     */
    public function isIosDevice()
    {
        return true;
    }

}


use Illuminate\Support\Facades\Notification;
use Alone\LaravelXiaomiPush\XiaomiNotification;

$msg = (new XiaomiNotification)
    ->title('通知标题')
    ->description('通知描述')
    ->body('通知描述 For iOS')
    ->payload([
         'action' => 'openApp',
    ])
    ->setHandler(function($msg,$notifiable,$cfg,$type = null)
    {
        /**
         * @link https://github.com/al-one/xmpush-php/blob/master/sdk/xmpush/Builder.php
         * @link https://github.com/al-one/xmpush-php/blob/master/sdk/xmpush/IOSBuilder.php
         */
        if($msg instanceof \xmpush\IOSBuilder)
        {
            $msg->badge(1);
        }
        elseif($msg instanceof \xmpush\Builder)
        {
            $msg->notifyId(rand(0,4));
        }
        return $msg;
    });

$user->notify($msg);
Notification::send($users,$msg);