Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Notification·¢ËͶÌÐź͵ç×ÓÓʼþ£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel notification·¢ËͶÌÐź͵ç×ÓÓʼþ£¿
LaravelÊÇÒ»¸öÊ¢ÐеĻùÓÚPHPµÄWebÓ¦ÓóÌÐò¿ò¼Ü£¬ËüÌṩÁË´ó×ڵĹ¦Ð§ºÍ¹¤¾ßÀ´¼ò»¯WebÓ¦ÓóÌÐò¿ª·¢¡£Laravel NotificationÊÇLaravel¿ò¼ÜµÄÒ»ÏîÖ÷Òª¹¦Ð§£¬ËüÌṩÁËÒ»ÖÖ¼òÆÓ¶ø¿ì½ÝµÄ·½·¨À´·¢ËÍ֪ͨ£¬°üÀ¨¶ÌÐź͵ç×ÓÓʼþ֪ͨ¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÉîÈë̽ÌÖÔõÑùʹÓÃLaravel NotificationÀ´·¢ËͶÌÐź͵ç×ÓÓʼþ֪ͨ¡£
µÚÒ»²½£º×°ÖÃÐëÒªµÄÒÀÀµ
ÔÚ×îÏÈ֮ǰ£¬ÎÒÃÇÐèҪװÖÃÁ½¸ö±ØÐèµÄÒÀÀµ£ºLaravel¿ò¼ÜºÍLaravel Notification×é¼þ¡£
ÈôÊÇÄúÒѾÓÐÒ»¸öLaravelÓ¦Óã¬ÄÇôÄú¿ÉÒÔÌø¹ýÕâÒ»²½£¬²»È»£¬Çë·¿ªÖն˲¢ÊäÈëÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öеÄLaravelÓ¦ÓóÌÐò£º
composer create-project --prefer-dist laravel/laravel my-app
µÇ¼ºó¸´ÖÆ
Ò»µ©½¨ÉèÁËÓ¦ÓóÌÐò£¬ÎÒÃÇ¿ÉÒÔ×°ÖÃLaravel Notification×é¼þ£¬Ö»ÐèÒªÔÚÖÕ¶ËÖÐÔËÐÐÒÔÏÂÏÂÁ
composer require laravel-notification-channels/nexmo
µÇ¼ºó¸´ÖÆ
´ËÏÂÁװÖÃnexmo֪ͨÇý¶¯³ÌÐò£¬ÕâÊÇÒ»ÖÖ¿ÉÓÃÓÚ·¢ËͶÌÐźÍÓïÒô֪ͨµÄ֪ͨÇý¶¯³ÌÐò¡£
µÚ¶þ²½£º½¨Éè֪ͨÀà
ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö֪ͨÀàÀ´½ç˵Ҫ·¢Ë͵ĶÌÐź͵ç×ÓÓʼþ֪ͨ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÔÚLaravelÓ¦ÓóÌÐòÖн¨ÉèÒ»¸öеÄ֪ͨÀࣺ
php artisan make:notification NewOrder
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áÔÚapp/NotificationsĿ¼Öн¨ÉèÒ»¸öÃûΪNewOrder.phpµÄÐÂ֪ͨÀà¡£Õâ¸öÀà½ç˵ÁË֪ͨµÄÄÚÈݺͷ¢ËÍ·½·¨¡£
ÈÃÎÒÃÇÀ´¿´¿´ÔõÑù½çËÃ÷ÈÕâ¸ö֪ͨÀà¡£
ÔÚtoNexmoÒªÁìÖУ¬ÎÒÃǽç˵ÁË·¢ËÍ֪ͨµÄÄÚÈݺͷ½·¨¡£ÈôÊÇÐèÒª·¢ËͶÌÐÅ£¬ÎÒÃÇ¿ÉÒÔ½«ÒÔÏÂÄÚÈÝÌí¼Óµ½Õâ¸öÒªÁ죺
return (new NexmoMessage) ->content('ÄãÊÕµ½ÁËÒ»Ìõж©µ¥¡£');
µÇ¼ºó¸´ÖÆ
ÈôÊÇÐèÒª·¢Ë͵ç×ÓÓʼþ֪ͨ£¬Ôò¿ÉÒÔ½«ÒÔÏÂÄÚÈÝÌí¼Óµ½Õâ¸öÒªÁìÖУº
return (new MailMessage) ->line('ÄãÊÕµ½ÁËÒ»Ìõж©µ¥¡£') ->action('Éó²é¶©µ¥', url('/orders')) ->line('ллÄãµÄʹÓá£');
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔƾ֤ÐèÒªÔÚ֪ͨÀàÖÐÌí¼ÓÆäËûÒªÁ죬ÀýÈ磺cc£¬bccºÍreplyTo¡£
µÚÈý²½£º·¢ËÍ֪ͨ
ÎÒÃÇÒѾ½ç˵ÁË֪ͨÀ࣬ÏÖÔÚÎÒÃÇÐèÒªÔÚÓ¦ÓóÌÐòÖз¢ËÍ֪ͨ¡£LaravelÌṩÁ˼¸ÖÖ²î±ðµÄ·½·¨À´·¢ËÍ֪ͨ£¬°üÀ¨È«¾Ö¹ã²¥¡¢ÐÐÁкÍÊÂÎñ¡£
ÈÃÎÒÃÇ¿´¿´ÔõÑùÔÚ¿ØÖÆÆ÷Öз¢ËÍ֪ͨ¡£
ÏÂÃæÊÇÒ»¸ö¼òÆӵĿØÖÆÆ÷´úÂëƬ¶Ï£¬ÎÒÃÇ¿ÉÒÔÏò¿Í»§¶Ë·¢ËÍÒ»Ìõж©µ¥Í¨Öª£º
namespace AppHttpControllers; use AppHttpControllersController; use IlluminateHttpRequest; use IlluminateSupportFacadesNotification; use AppNotificationsNewOrder; class OrderController extends Controller { public function store(Request $request) { // ½¨Éèж©µ¥µÄ´úÂë... $user = auth()->user(); Notification::send($user, new NewOrder($order)); } }
µÇ¼ºó¸´ÖÆ
ÔÚÿ¸öж©µ¥µÄ½¨Éè´úÂëÔËÐкó£¬ÎÒÃÇ¿ÉÒÔʹÓÃ֪ͨÀàNewOrderÀ´Ïò¿Í»§¶Ë·¢ËÍж©µ¥Í¨Öª¡£ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃLaravelµÄNotificationÃÅÃæÀ´·¢ËÍ֪ͨ¡£
µÚËIJ½£º²âÊÔ֪ͨ
ÎÒÃÇÒѾ±àдÁË֪ͨÀàºÍ¿ØÖÆÆ÷£¬²¢Ê¹ÓÃLaravelµÄNotificationÃÅÃæ·¢ËÍÁË֪ͨ£¬ÏÖÔÚÎÒÃÇÐèÒª²âÊÔ֪ͨÊÇ·ñ¿ÉÒÔ׼ȷ·¢ËÍ¡£
ÈÃÎÒÃÇÏȲâÊÔ·¢Ë͵ç×ÓÓʼþ֪ͨ¡£ÔÚ֪ͨÀàÖÐÌí¼ÓÒÔÏÂÒªÁìÀ´ÉèÖõç×ÓÓʼþ²âÊÔÎüÊÕÕߵĵç×ÓÓʼþµØµã£º
public function toMail($notifiable) { return (new MailMessage) ->to('test@example.com') ->line('ÄãÊÕµ½ÁËÒ»Ìõж©µ¥¡£') ->action('Éó²é¶©µ¥', url('/orders')) ->line('ллÄãµÄʹÓá£'); }
µÇ¼ºó¸´ÖÆ
Note£ºÕâÊÇÒ»¸ö²âÊÔµç×ÓÓʼþµØµã£¬ÄãÐèҪƾ֤×Ô¼ºµÄÐèÇóÉèÖá£
ÏÖÔÚÎÒÃÇ¿ÉÒÔÔٴαàд´úÂëÀ´´¥·¢Í¨Öª£¬ÓòâÊÔµç×ÓÓʼþÎüÊÕÆ÷À´È¡´úÏÖʵ¿Í»§¶Ë£º
Notification::route('mail', 'test@example.com') ->notify(new NewOrder($order));
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ»¹¿ÉÒÔ²âÊÔ·¢ËͶÌÐÅ֪ͨ£¬ÔÚ֪ͨÀàÖÐÌí¼ÓÒÔÏÂÐÐÀ´ÉèÖöÌÐŲâÊÔÎüÊÕÕߵĵç×ÓÓʼþµØµã£º
public function toNexmo($notifiable) { return (new NexmoMessage) ->to('+1234567890') ->content('ÄãÊÕµ½ÁËÒ»Ìõж©µ¥¡£'); }
µÇ¼ºó¸´ÖÆ
Note£ºÕâÊÇÒ»¸ö²âÊԵ绰ºÅÂ룬ÄãÐèҪƾ֤×Ô¼ºµÄÐèÇóÉèÖá£
ÏÖÔÚÎÒÃÇ¿ÉÒÔ±àд´úÂëÀ´´¥·¢Í¨Öª£¬ÓòâÊÔµç×ÓÓʼþÎüÊÕÆ÷À´Ìæ»»ÏÖʵ¿Í»§¶Ë£º
Notification::route('nexmo', '+1234567890') ->notify(new NewOrder($order));
µÇ¼ºó¸´ÖÆ
ÈôÊÇÄúÊÕµ½ÁËÒ»¸öµç×ÓÓʼþ»ò¶ÌÐÅ֪ͨ£¬ÄÇô¾Í֤ʵÄãÒѾÀֳɵØʹÓÃÁËLaravel Notification·¢ËÍ֪ͨ¡£
ʵÑéʹÓÃLaravel NotificationÀ´·¢ËͶÌÐź͵ç×ÓÓʼþ֪ͨ£¬Ï£ÍûËüÄÜ×ÊÖúÄã¹¹½¨¸üºÃµÄWebÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Notification·¢ËͶÌÐź͵ç×ÓÓʼþ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡