App backend development 3: Use Baidu message push in laravel

Source: Internet
Author: User
App backend development 3: Use Baidu message push in laravel Introduction

Message pushing is often used during APP development. If an entrepreneurial company builds its own message pushing server, the time cost and technical difficulty will undoubtedly increase a lot. In my own practice, I think Baidu's message push service has a high stability as a whole, and the interface documentation is also very complete, which is recommended for use.
* The message push service in this article uses Baidu message push SDK 3.0.0
* Laravel version: 5.1 .*
* PHP> 5.5.9

Project

Project address: baidu-pusher

To facilitate the use of Baidu message push in multiple projects, Baidu message push is encapsulated into a composer package. You can use it after 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 complete, configure the providers array and the aliases array in config \ app. php.

'Providers '=> [/** third-party provider */Riverslei \ Pusher \ PusherServiceProvider: class,], 'providers '=> [/** Third Party */'push' => Riverslei \ Pusher: class,],

After the preceding configuration is complete, run the following command to generate the push 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 view the configuration information. Change to your own apikey and other content.

The default content is the account used for testing.

3. test the SDK

Baidu has never provided the server-side test code before and cannot perform independent tests. After this SDK upgrade, I finally got a test account. You can try it. The following is the test code that conforms to Laravel according to the official test code.

First, configure the route

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

Second, create the controller and the 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: pushMsgToSingleDevice ($ channelId, $ message, $ opts); // determines the return value. when sending fails, $ rs returns false. you can use getError to obtain error information. if ($ rs = false) {print_r (Pusher: getLastErrorCode (); print_r (Pusher: getLastErrorMsg ());} else {// prints the message id, sending time, and other information. var_dump ($ rs);} echo "done! ";}}

Then, access http: // youdomain/pusher in the browser. View content in the browser

This message is displayed, indicating that the operation is successful. It can be used at an appropriate location.

Others

When using message push in a project, we recommend that you put the push service into the queue. do not wait until the message push is complete in the business before returning the result to the APP. For details about how to use Laravle message queue, refer to this article.

For more information about the message push APIs, see Baidu message push documentation.

If you think this project is good, start is welcome.

Subsequent articles on using Laravel to develop app interfaces will be updated continuously. Thank you for your attention.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.