Home >PHP Framework >Laravel >How to push messages to APP in laravel

How to push messages to APP in laravel

(*-*)浩
(*-*)浩Original
2019-10-30 09:42:563615browse

In the process of APP development, message push is often used. For entrepreneurial companies, if they build their own message push servers, the time cost and technical difficulty will undoubtedly increase a lot. In my own practice, I feel that the overall stability of Baidu's message push service is very high, and the interface documentation is also very complete, so it is recommended to use it.

How to push messages to APP in laravel

* The message push service in this article uses Baidu Message Push SDK version 3.0.0

* Laravel version is: 5.1 .* (Recommended learning: laravel development)

* PHP>5.5.9

In order to facilitate the use of Baidu’s message push in multiple projects, we will Baidu message push package becomes composer package. You can use it through simple installation.

1. Add the following content to composer.json installed in the project

"require": {
        "riverslei/baidu-pusher": "~1.0"
    },

Then execute composer update

2. After the installation is completed, configure config\app.php The providers array and aliases array.

    'providers' => [
        /*
         * 第三方提供者
         */
        Riverslei\Pusher\PusherServiceProvider::class,
    ],

    'providers' => [
        /*
         * 第三方
         */
        'Pusher'    => Riverslei\Pusher\Pusher::class,
    ],

After the above configuration is completed, use the following command to generate the pushed configuration file

php artisan vendor:publish --provider=Riverslei\Pusher\PusherServiceProvider

After executing this command, a configuration file pusher.php will be added to the config folder. You can open it to view related configuration information. Modify it to your own apikey and other content.

The default content is for the test account.

3. Test SDK

Baidu has not provided server-side test code before, and has been unable to conduct independent testing. After the SDK upgrade this time, I finally got a test account. You can give it a try. The following is the test code I wrote based on the official test code that conforms to Laravel.

First, configure routing

Route::get('/pusher', 'TestController@pusher');

Secondly, create the controller and method

<?php

namespace App\Http\Controllers;

use Pusher;

class TestController extends Controller
{
    public function pusher()
    {
        $channelId = &#39;3785562685113372034&#39;;
        // 消息内容.
        $message = array (
                // 消息的标题.
                &#39;title&#39; => &#39;Hi!.&#39;,
                // 消息内容
                &#39;description&#39; => "hello!, this message from baidu push service."
        );
        // 设置消息类型为 通知类型.
        $opts = array (
                &#39;msg_type&#39; => 1
        );
        // 向目标设备发送一条消息
        $rs = Pusher::pushMsgToSingleDevice($channelId, $message, $opts);

        // 判断返回值,当发送失败时, $rs的结果为false, 可以通过getError来获得错误信息.
        if($rs === false){
            print_r(Pusher::getLastErrorCode());
            print_r(Pusher::getLastErrorMsg());
        }else{
            // 将打印出消息的id,发送时间等相关信息.
            var_dump($rs);
        }

        echo "done!";
    }
}

After completion, visit: http://youdomain/pusher in the browser. Check the content in the browser

How to push messages to APP in laravel

#If the content appears, it means it has been successful. Can be used in appropriate locations.

The above is the detailed content of How to push messages to APP in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to learn laravelNext article:How to learn laravel