![](/img/trans.png)
[英]How to subscribe user with laravel notification channel for OneSignal
[英]Laravel notification add mention of user in group channel
我正在嘗試在通用頻道中發送提及用戶的通知。 這就是我所擁有的:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class WeeklyTasksResponsible extends Notification
{
use Queueable;
protected $employee;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(\App\Employee $employee)
{
$this->employee = $employee;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->content('Reponsible for this week is: ' . $this->employee->slack_name);
}
}
這將在我們公司的通用 Slack 頻道中發送每周通知。 消息是“本周的負責人是:nameofuser”。 問題是用戶沒有看到此通知。
我也試過這樣做:
public function toSlack($notifiable)
{
return (new SlackMessage)
->content('Reponsible for this week is: @' . $this->employee->slack_name);
}
但這與在頻道中提及我自己不同。
我怎樣才能做到這一點?
正如@HCK提到的,你可以使用戶名的匹配@username
中提到chat.postMessages
通過可選的參數設置link_names
至true
。
但是,不推薦使用用戶名創建提及項,不應再使用。
推薦的方法是使用用戶 ID 創建提及,默認情況下會起作用。
例子:
<@U12345678>
有關用戶名棄用的詳細信息,請參閱 Slack 對用戶名的揮之不去的告別一文。
我剛剛在 Laravel 6.18.0 源代碼中發現了以下內容:
/**
* Find and link channel names and usernames.
*
* @return $this
*/
public function linkNames()
{
$this->linkNames = 1;
return $this;
}
vendor/laravel/slack-notification-channel/src/Messages/SlackMessage.php
所以你可以像這樣使用它:
public function toSlack($notifiable)
{
return (new SlackMessage)
->linkNames()
...
}
正如@erik-kalkoken 指出的那樣,使用 Slack 用戶 ID,用<>
括起來並用@
符號括起來。 您可以在 Slack 應用程序的個人資料中找到它:
實際上,我嘗試過這種方式並且效果很好。
$content .= "New order!\r\n @person";
return (new SlackMessage)
->content($content)->linkNames();
您需要在人員或團隊的名稱之前添加@並且為了讓 slack 將它們識別為提及而不僅僅是文本,您需要使用linkNames鏈接內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.