<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script> <script> var OneSignal = OneSignal || []; OneSignal.push(["init", { appId: " id ", subdomainName: 'laravel-news', // onesignal.com ( ) notifyButton: { enable: true, // Set to false to hide, size: 'large', // One of 'small', 'medium', or 'large' theme: 'default', // One of 'default' (red-white) or 'inverse" (whi-te-red) position: 'bottom-right', // Either 'bottom-left' or 'bottom-right' offset: { offset: { bottom: '90px', left: '0px', // Only applied if bottom-left right: '80px' // Only applied if bottom-right }, text: { "tip.state.unsubscribed": " ", "tip.state.subscribed": " ", "tip.state.blocked": " ", "message.prenotify": " ", "message.action.subscribed": " !", "message.action.resubscribed": " ", "message.action.unsubscribed": ", ", "dialog.main.title": " ", "dialog.main.button.subscribe": "", "dialog.main.button.unsubscribe": " ", "dialog.blocked.title": " ", "dialog.blocked.message": " , :" } }, prenotify: true, // Show an icon with 1 unread message for first-time site visitors showCredit: false, // Hide the OneSignal logo welcomeNotification: { "title": " Laravel", "message": " !" }, promptOptions: { showCredit: false, // Hide Powered by OneSignal actionMessage: " :", exampleNotificationTitleDesktop: " ", exampleNotificationMessageDesktop: " ", exampleNotificationTitleMobile: " ", exampleNotificationMessageMobile: " ", exampleNotificationCaption: "( )", acceptButtonText: "".toUpperCase(), cancelButtonText: ", ".toUpperCase() } }]); </script>
Of course, there was a spoiler in the title about the events, so everyone guessed right away)
protected $listen = [ 'App\Events\PostPublishedEvent' => [ 'App\Listeners\PostActionsListener', ], ];
php artisan event:generate
public $post; /** * PostPublishedEvent constructor. * @param Post $post */ public function __construct(Post $post) { $this->post = $post; }
use App\Models\Post;
/** * Handle the event. * * @param Event $event * @return void */ public function handle(Event $event) { if ($event instanceof PostPublishedEvent) { // } }
const STATUS_DRAFT = 0; const STATUS_PUBLISHED = 1;
php artisan make:migration add_noty_status_to_post_table --table=post
public function up() { Schema::table('post', function (Blueprint $table) { $table->tinyInteger('notify_status')->default(0); }); }
php artisan migrate
console php artisan migrate
public static function boot() { static::saving(function($instance) { // – «», , «» if ($instance->status == self::STATUS_PUBLISHED && $instance->notify_status < self::STATUS_PUBLISHED){ // «» $instance->notify_status = self::STATUS_PUBLISHED; // «» PostPublishedEvent, . \Event::fire(new PostPublishedEvent($instance)); }); parent::boot(); }
$factory->define(App\Models\Post::class, function (Faker\Generator $faker) { return [ 'title' => $faker->text(100), 'publish_date' => date('Ymd H:i'), 'short_text' => $faker->text(300), 'full_text' => $faker->realText(1000), 'slug' => str_random(50), 'status' => \App\Models\Post::STATUS_PUBLISHED, 'category_id' => 1 ]; });
class PostCreateTest extends TestCase { public function testPublishEvent() { //, \App\Events\PostPublishedEvent $this -> expectsEvents(\App\Events\PostPublishedEvent::class); // $post = factory(App\Models\Post::class)->create(); // $this -> seeInDatabase('post', ['title' => $post->title]); // $post -> delete(); } }
Please note: when testing events, the events themselves do not occur. Only the fact of their occurrence or non-occurrence is recorded.
OK (1 test, 1 assertion)
public function testNoPublishEvent() { $this->doesntExpectEvents(\App\Events\PostPublishedEvent::class); // – status. $post = factory(App\Models\Post::class)->create( [ 'status' => App\Models\Post::STATUS_DRAFT ]); $this->seeInDatabase('post', ['title' => $post->title]); $post->delete(); }
OK (2 tests, 2 assertions)
composer require anlutro/curl
console, composer require anlutro/curl
<?php namespace App\Handlers; use anlutro\cURL\cURL; use App\Models\Post; class OneSignalHandler { // private $test = false; // " " public function __construct($test=false) { $this->test = $test; } // sendNotify . public function sendNotify(Post $post) { // $config = \Config::get('onesignal'); // app_id , if (!empty($config['app_id'])) { //C $data = array( 'app_id' => $config['app_id'], 'contents' => [ "en" => $post->short_text ], 'headings' => [ "en" => $post->title ], //( WebPush ) 'isAnyWeb' => true, 'chrome_web_icon' => $config['icon_url'], 'firefox_icon' => $config['icon_url'], 'url' => $post->link ); // test == true , if ($this->test) { $data['include_player_ids'] = [$config['own_player_id']]; } else { // - . $data['included_segments'] = ["All"]; } // ! ! if (strtotime($post->publish_date) > time()) { $data['send_after'] = date(DATE_RFC2822, strtotime($post->publish_date)); $data['delayed_option'] = 'timezone'; $data['delivery_time_of_day'] = '10:00AM'; } $curl = new cURL(); $req = $curl->newJsonRequest('post',$config['url'], $data)->setHeader('Authorization', 'Basic '.$config['api_key']); $result = $req->send(); // , . if ($result->statusCode <> 200) { \Log::error('Unable to push to Onesignal', ['error' => $result->body]); return false; } $result = json_decode($result->body); if ($result->id) { // - - . return $result->recipients; } } } }
<?php return [ 'app_id' => env('ONESIGNAL_APP_ID',''), 'api_key' => env('ONESIGNAL_API_KEY',''), 'url' => env('ONESIGNAL_URL','https://onesignal.com/api/v1/notifications'), 'icon_url' => env('ONESIGNAL_ICON_URL',''), 'own_player_id' => env('ONESIGNAL_OWN_PLAYER_ID','') ];
ONESIGNAL_APP_ID = 256aa8d2…. ONESIGNAL_API_KEY = YWR….. ONESIGNAL_ICON_URL = http://laravel-news.ru/images/laravel_logo_80.jpg ONESIGNAL_URL = https://onesignal.com/api/v1/notifications ONESIGNAL_OWN_PLAYER_ID = 830…
public function testSendOnesignal() { // ( ) $post = factory(App\Models\Post::class)->make(); // test = true $handler = new \App\Handlers\OneSignalHandler(true); // $result = $handler->sendNotify($post); // 1, . $this->assertEquals(1,$result); }
phpunit
console - the test passes successfully and a notification pops up (sometimes there are delays of up to several minutes) /** * Handle the event. * * @param Event $event * @return void */ public function handle(Event $event) { if ($event instanceof PostPublishedEvent) { (new OneSignalHandler())->sendNotify($event->post); } }
Source: https://habr.com/ru/post/279385/
All Articles