App Backend Development III: Laravel using Baidu's message push

Source: Internet
Author: User
Introduction

Message pushes are often used during app development. For entrepreneurial companies to build their own messaging servers, time costs, and technical difficulties will undoubtedly increase a lot. In my own practice process, I feel that Baidu's message push service stability is very high overall, and the interface documentation is also very perfect, recommended use.
* Text message Push service using Baidu message Push SDK 3.0. version 0
* The Laravel version is: 5.1.*
* php>5.5.9

Project

Project Address: Baidu-pusher

In order to facilitate the use of a number of projects Baidu message push, so Baidu message push package became composer package. It can be used with a simple installation.

1. Add the following to the Composer.json installed in the project

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

Then perform composer update

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

    ' providers ' + [/        * * Third-party provider */        Riverslei\pusher\pusherserviceprovider::class,], ' providers ' = [/* * * Third party */' pusher ' = Riverslei\pusher\pusher::class,],

Once the above configuration is complete, use the command to generate the push configuration file

PHP Artisan vendor:publish--provider=riverslei\pusher\pusherserviceprovider

After the command is executed, a profile pusher.php is added to the Config folder. You can open the view related configuration information. Modify it for your own apikey and other content.

The default content is the account used for the test.

3. Testing the SDK

Before Baidu has not provided the service end of the test code, has been unable to conduct independent testing. This time after the SDK upgrade, finally got a test account. We can try it. Below is the test code that I write according to the official test code, which conforms to laravel.

First, configure the routing

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

Second, create the controller and method

 
  ' hi! ',                //message content                ' description ' + ' hello!, this message from Baidu Push service. "        );        Set the message type to the notification type.        $opts = Array (                ' msg_type ' = 1        );        Send a message to the target device        $rs = pusher::p ushmsgtosingledevice ($channelId, $message, $opts);        The return value is determined, and when the send fails, the $RS result is false, and the error message can be obtained by GetError.        if ($rs = = = False) {            Print_r (Pusher::getlasterrorcode ());            Print_r (Pusher::getlasterrormsg ());        } else{            //will print out the message ID, send time and other related information.            Var_dump ($RS);        }        echo "done!";    }}

When finished, access in the browser: Http://youdomain/pusher. View content in a browser

The content appears, indicating that it has been successful. Can be used in the right place.

other

When using message push in your project, it is recommended that you put the push business in the queue, and do not wait for the message push to complete in the business before returning the results to the app. Please refer to this article for the use of Laravle Message Queuing.

In the process of use, the message push interface please refer to the Baidu message push document

If you think the project is good, welcome to start, Project address

Subsequent articles about using Laravel to develop the app interface will be updated continuously. Thank you for your attention.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.