【问题标题】:How to make a notification to the dynamic cell phone with Laravel?如何使用 Laravel 向动态手机发送通知?
【发布时间】:2021-05-16 09:24:21
【问题描述】:

我的代码让管理员可以向群组发送消息,很快该群组中的所有用户都会在移动应用上收到通知:

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
    }
}

【问题讨论】:

  • 您能否显示通知的 __construct 函数,以便我们检查您传递的消息会发生什么情况?目前尚不清楚该消息会发生什么,也不清楚发票的定义位置。

标签: php laravel firebase notifications


【解决方案1】:

我决定颠倒谁是我的通知的逻辑,现在我的消息不是用户是通知,而是

在我的控制下我更简单:

    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 中开始接收消息的信息,然后很容易将它们作为参数传递。

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多