php - 如何使用 Laravel 向动态手机发送通知?
问题描述
我编写了我的代码,管理员可以在其中向组发送消息,很快该组中的所有用户都会在移动应用程序上收到通知:
public function create(Request $request)
{
if(!($error = $this->isNotValidate($request))){
//Log::error(print_r("validado", true));
$message = Message::create($request->toArray());
$message->recives()->attach($request->recive_ids);
foreach($message->recives as $group){
foreach($group->users as $user){
$user->notify(new NotificationMessage($message));
}
}
$response = ['message' => $message];
return response($response, 200);
}
return response($error, 422);
}
我使用了该库:https ://github.com/laravel-notification-channels/fcm它正在工作,他通过firebase将通知发送到手机。
我的困难是在 toFcm 函数中我想检索消息以构建我发送的通知:
public function toFcm($notifiable)
{
return FcmMessage::create()
->setData(['message_id' => $this->invoice->id, 'message_created' => $this->invoice->created_at])
->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
->setTitle($this->invoice->title)
->setBody($this->invoice->content)
//->setImage('http://example.com/url-to-image-here.png')
)->setAndroid(
AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
)->setApns(
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}
我认为在变量$this->invoice
中或$notifiable
将出现$message
我在创建中作为参数传递的,但它没有通过,两者都返回用户。
有谁知道我如何用我的消息数据制作这个动态通知?
更新
class NotificationMessage extends Notification
{
/**
* Specifies the user's FCM token
*
* @return string|array
*/
public function routeNotificationForFcm()
{
return $this->fcm_token;
}
public function via($notifiable)
{
return [FcmChannel::class];
}
public function toFcm($notifiable)
{
return FcmMessage::create()
->setData(['message_id' => $notifiable->id, 'message_created' => $notifiable->created_at])
->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
->setTitle($notifiable->title)
->setBody($notifiable->content)
//->setImage('http://example.com/url-to-image-here.png')
)->setAndroid(
AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
)->setApns(
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}
// optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
public function fcmProject($notifiable, $message)
{
// $message is what is returned by `toFcm`
return 'app'; // name of the firebase project to use
}
}
解决方案
我决定颠倒谁是我的通知的逻辑,现在我的消息不是通知用户,而是
在我的控制下,我更简单:
public function create(Request $request)
{
if(!($error = $this->isNotValidate($request))){
//Log::error(print_r("validado", true));
$message = Message::create($request->toArray());
$message->recives()->attach($request->recive_ids);
$message->notify(new NotificationMessage);
$response = ['message' => $message];
return response($response, 200);
}
return response($error, 422);
}
在我的消息模板中,我做了以下更改:
use Illuminate\Notifications\Notifiable;
...
class Message extends BaseModel
{
use Notifiable;
...
public function routeNotificationForFcm()
{
$tokens = [];
foreach($this->recives as $group){
foreach($group->users as $user){
$tokens = array_merge($tokens, $user->notifications()->pluck('token')->toArray());
}
}
return $tokens;
}
}
在我的变量中$notifiable
开始接收消息的信息,然后很容易将它们作为参数传递。
推荐阅读
- javascript - 从反应中的一组对象计算发票总额和发票数量
- stripe-payments - 条带结帐测试返回 404
- c++ - 关于在赋值中从 double 到 int 的隐式转换的标准
- javascript - 如何从我点击的元素中选择内容(使用数据属性)
- xml - 是否有任何用于 Spark 的 XML 输入的模式生成器?
- javascript - 在弹出式灯箱中更新 Swiper
- javascript - 如何在 React 中使用 ref 访问 onDrag onDrop 函数?
- javascript - 无法从 fetch 中输出数据
- c - SAME70 在 ISR 中读取外部中断极性
- java - 如何将带有列表视图的 EditText 滚动到片段中布局的最顶部?