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.