首页 > 解决方案 > 如何使用 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
    }
}

标签: phplaravelfirebasenotifications

解决方案


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

在我的控制下,我更简单:

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


推荐阅读